class A contain:
public static ArrayList<String> sourceList = new ArrayList<String>();
// here ArrayList contain some string type data;
public static ArrayList<String> getSource()
{
return sourceList;
}
main class contain:
List<String> s1 = A.getSource();
for(String dk:s1) {
System.out.println(dk);
}
Here I am storing ArrayList data of class A into a list in class B and checking
whether its works or not.but when I run the main class it gives nothing.
thanks.
I've simulated what you are doing, and it's working as expected. Are you sure the list is being populated correctly?
import java.util.ArrayList;
import java.util.List;
public class PrintList {
public static void main(String[] args) {
List<String> s1 = AClass.getSource();
for (String dk : s1) {
System.out.println("We have : " + dk);
}
}
}
class AClass {
public static final ArrayList<String> sourceList = new ArrayList<String>();
static {
sourceList.add("A string");
sourceList.add("Another string");
}
public static ArrayList<String> getSource() {
return sourceList;
}
}
If that is all the code you are using, then you will never get anything since you are never populating the array list in the first place.
If you really want to see if it is working, try something like so:
public static ArrayList<String> sourceList = new ArrayList<String>();
// here ArrayList contain some string type data;
public static ArrayList<String> getSource()
{
sourceList.add("Hello I am working");
return sourceList;
}
If all goes well, you should see the string Hello I am working.
Related
I think it's a simple question but keeps me from moving forward in the project. Why, when I use the ArrayList as a reference from a class to another, it is empty?
public class FirstClass {
private ArrayList<String> myList = new ArrayList<>();
public ArrayList<String> getMyList() {
return myList;
}
public static void main(String[] args) {
FirstClass fc = new FirstClass();
fc.getMyList().add("Test");
System.out.println(fc.getMyList());
}
}
Output when I run FirstClass.main:
[Test]
public class OtherClass {
FirstClass csv = new FirstClass();
public ArrayList<String> otherList = csv.getMyList();
public static void main(String[] args) {
OtherClass oc = new OtherClass();
System.out.println(oc.otherList);
}
}
Output when running the OtherClass.main:
[]
I've Googled so many time and the solutions that I found are not working. I followed step by step, creating a getter method in the class that is populating the ArrayList, use the method to populate it, use the print method to make sure the array is populated at the end of the main method. All good, ArrayList is full and ready to be used but when used as reference is empty.
Because a new instance of FirstClass is being created inside of OtherClass, otherList in OtherClass is empty.
The ArrayList that oc.otherList refers to is different from the one that you populated for FirstClass in the main method.
You can edit your OtherClass to work:
public class OtherClass {
private FirstClass csv;
public ArrayList<String> otherList;
public OtherClass(FirstClass fc) {
this.csv = fc;
this.otherList = fc.getMyList();
}
public static void main(String[] args) {
FirstClass fc = new FirstClass();
fc.getMyList().add("Test");
OtherClass oc = new OtherClass(fc);
System.out.println(oc.otherList);
}
}
OtherClass creates the FirstClass without FirstClass main method, so it does not add() to the list.
You can do something like below.
public class FirstClass {
private List<String> myList;
// Instance block: will executed the moment you create object
// of FirstClass
{
myList = new ArrayList<>();
myList.add("Test String");
}
public List<String> getMyList() {
return myList;
}
}
public class OtherClass {
private static List<String> otherList;
public static void main(String[] args) {
FirstClass fc = new FirstClass();
otherList = fc.getMyList();
System.out.println(otherList); //iterate if want to check specific elements in the list
}
}
Or a better way would be to create a method which will add elements to the list and read it in the otherclass by calling the method using firstclass's object.
I'm a newbie in java and I have to do a "array project" for college. I'm having trouble getting the array in my Main class from Contacts class where it is stored. This is what I've done for now:
public class Main {
public static void main(String[] args) {
// View Contact Testing
Contacts contactObj = new Contacts();
System.out.println(contactObj);
}
}
public class Contacts {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
arr.add("name1");
arr.add("name2");
return arr.clone();
}
}
When I run this, I get this error: Error:(11, 25) java: incompatible types: unexpected return value.
Any help would be much appreciated,
Thanks
As suggested by others I also advice you study a little bit more in some basic Java tutorials (there is a several spread over www). But also I understanding your struggling and will give you a few example that how you could do that. Follow:
Main.java
public class Main {
public static void main(String[] args) {
Contacts contactObj = new Contacts();
contactObj.getNames().forEach(System.out::println);
contactObj.cleanNames();
contactObj.getNames().forEach(System.out::println);
contactObj.addName("John");
contactObj.addName("Maria");
contactObj.getNames().forEach(System.out::println);
}
}
Contacts.java
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Contacts {
private List<String> names;
//either to help your understanding and for sake of simplicity
public Contacts() {
this.names = new ArrayList<String>();
this.names.add("name1");
this.names.add("name2");
}
//clean the content of names from names List
public void cleanNames(){
if (Objects.nonNull(names)) {
this.names.clear();
}
}
//add a name to list
public void addName(String name) {
if (Objects.nonNull(names)) {
this.names.add(name);
} else {
this.names = new ArrayList<>();
this.names.add(name);
}
}
//set a entire list to names attribute
public void setNames(List<String> names) {
this.names = names;
}
//get the name attribute
public List<String> getNames() {
if (Objects.nonNull(names)) {
return this.names;
} else {
this.names = new ArrayList<>();
return names;
}
}
}
import java.util.*;
class Pilot
{
protected String PILOT = "BSIT-1A";
public static void Subject()
{
String[] subs = {"Comprog11","WebDev","Digilog12","ComProg12"};
}
public static void Teacher()
{
String[] teach = {"Ms.a","Ms.b","Ms.c","Ms.d"};
}
}
class Pilot1 extends Pilot
{
protected String PILOT1 = "BSIT-1B";
public static void main(String[]args)
{
Pilot1 obj = new Pilot1();
System.out.println(obj.PILOT);
System.out.println(obj.PILOT1);
obj.Subject();
obj.Teacher();
}
how to display the values of Subject() and Teacher() if I put inside it a String?It doesnt have any compiler issues but when I ran it display only the
BSIT-1A
BSIT-1B
my expected output is
BSIT-1A
BSIT-1B
Comprog11
Webdev
Digilog12
Comprog12
Ms.a
Ms.b
Ms.c
Ms.d
You can return the array
public String[] subject() {
return {"Comprog11","WebDev","Digilog12","ComProg12"};
}
Then remove the inheritance. You don't need it for the main method. Make the pilot constants public or pass those strings into a class constructor and add a private field with a getter method, for example
Pilot a = new Pilot("BSIT-1A");
Pilot b = new Pilot("BSIT-1B");
System.out.println(a.getCode());
System.out.println(b.getCode());
Arrays.stream(a.subject()).forEach(System.out::println);
You need the either print the values in Subject() and Teacher() or make them return the values. Also calling static methods via objects is not a
good practice. They should be invoked by the class, like Pilot1.Subject().
class Pilot
{
protected String PILOT = "BSIT-1A";
public static void Subject()
{
String[] subs = {"Comprog11","WebDev","Digilog12","ComProg12"};
for(String sub : subs){
System.out.println(sub);
}
}
public static void Teacher()
{
String[] teach = {"Ms.a","Ms.b","Ms.c","Ms.d"};
for(String t : teach){
System.out.println(t);
}
}
}
Okay, so I have an arraylist declared in my main because elsewhere it brings up an error. However, I want to use that arraylist later specifically to have a getter, but it doesn't recognize the arraylist because it is in my main.
The error is
"it cannot be resolved to a variable"
.
What can I do to correct this?
public static void main(String[] args) {
ArrayList <String> Strings = new ArrayList <String>();
Strings.add("hi");
Strings.add("hello");
Strings.add("goodbye");
}
public ArrayList<String> getArrList() {
return Strings;
}
You need to make use of the OOP, dont define things static if not needed, use setters and getters and encapsulate the private fields of the classs
Example:
public class Tester {
private List<String> stringList;
public Tester() {
stringList = new ArrayList<String>();
}
public void populateList() {
stringList.add("hi");
stringList.add("hello");
stringList.add("goodbye");
}
public static void main(String[] args) {
Tester t = new Tester();
t.populateList();
List<> list = t.getList();
System.out.println( list );
}
public List<String> getList(List<String> list) {
return stringList;
}
public List<String> setList() {
return stringList;
}
}
You can't call non-static method from static method.
You must define your arraylist as a static variable, then make its getter as static.
static ArrayList <String> Strings;
public static void main(String[] args) {
Strings = new ArrayList <String>();
Strings.add("hi");
Strings.add("hello");
Strings.add("goodbye");
}
public static ArrayList<String> getArrList() {
return Strings;
}
Do like this,
class MyClass{
ArrayList <String> Strings;
public static void main(String[] args) {
Strings = new ArrayList <String>();
Strings.add("hi");
Strings.add("hello");
Strings.add("goodbye");
}
public ArrayList<String> getArrList() {
MyClass myClass = new myClass();
return myClass.Strings;
}
}
I have the following class which stores a list of object arrays.
public class Test {
private List<Object[]> list = new ArrayList<Object[]>();
public void addList(Object... obj) {
list.add(obj);
}
public void addList(List<Object> lst) {
list.add(lst.toArray());
}
}
When I call the following, the overloaded method addList(Object... obj) is called but I want the addList(List<Object> lst) to be called. How can I do this?
public class Main {
public static void main(String[] args) {
Test testObj = new Test();
List<String> myStrings = new ArrayList<String>();
myStrings.add("string 1");
myStrings.add("string 2");
myStrings.add("string 3");
// The variable argument method is called but this is a list!
testObj.addList(myStrings);
}
}
Change List<Object> to List<?> to capture lists of any type of object. I tried this and it printed "in List":
import java.util.ArrayList;
import java.util.List;
public class Test {
private List<Object[]> list = new ArrayList<Object[]>();
public void addList(Object... obj) {
System.out.println("in object");
list.add(obj);
}
public void addList(List<?> lst) {
System.out.println("in List<?>");
list.add(lst.toArray());
}
public static void main(String[] args) {
Test testObj = new Test();
List<String> myStrings = new ArrayList<String>();
myStrings.add("string 1");
myStrings.add("string 2");
myStrings.add("string 3");
// The variable argument method is called but this is a list!
testObj.addList(myStrings);
}
}
It's problem of Java Generic. You cannot assign List<String> to List<Object>.
See also: Java Reference assignment with generic lists
Rewrite the type of you non-variadic method to use a wildcard:
public void addList(List<?> lst) {
list.add(lst.toArray());
}
Then List<String> will be a subtype of the parameter type.
List<String> is not a subclass of List<Object>. So that overload will never be called, even if you remove the ... variant.
Change your method to
public void addList(List<?> lst) {
list.add(lst.toArray());
}