I have a class and this class has one public element ArrayList myList.(I have other elements too in my class but they have nothing to do with this element)
What I am trying to do is having different ArrayLists for different Class Objects. However when I tried to code this, even if I use different class objects, the code reserves every entry in one single ArrayList. What am i doing wrong?
This is what i tried: My class:
public class myClass {
public static ArrayList myList;
public static ArrayList getList() {
return myList;
}
public static void setList(ArrayList myList) {
myClass.myList = myList;
}
In main:
myClass my = new myClass();
myClass my2 = new myClass();
ArrayList tmp = new ArrayList();
ArrayList tmp2 = new ArrayList();
tmp.add("aaaaa");
tmp.add("bbbbb");
tmp2.add("ccccc");
tmp2.add("ddddd");
my.setList(tmp);
my2.setList(tmp2);
for(int i=0;i<my.getList().size();i++)
{
System.out.println(my.getList().get(i));
}
And the output of this main is being:
ccccc
ddddd
but i want it to be:
aaaaa
bbbbb
What am i doing wrong?