hi,
my arraylistS in a covering arraylist behave like the same instance.

i manupulate one of them with

i=0; manupulate(myarrofarrs.get(i));

, and all the sub arraylists have become effected of the manupulation.

i googled a little but in google it was said: "create new instance with myvar=new myobject()"

i have already done so.

here is the code:

ArrayList<MyObj> geciciListe=new ArrayList<MyObj>();
        while(some condition){
            tempList=new ArrayList<MyObj>();//here is the advise
            i=0;
            while(i<max){
                if(some condition){
                tempList.add(myObjarray[i]);
                }else{
                templist.add(myotherobjarray[i]);
                }
                i++;
            }
            theCoveringRootArraylist.add(templist);
            

        }

after this loop i manupulate by the way:

while(){
 ArrayList<MyObj> a=new ArrayList<MyObj>();
 a=(ArrayList)theCoveringRootArraylist.get(i);
 manupulate(a);//
 theCoveringRootArrayList.set(i,a);
}

as you can see i do everything to make feel them they are different instances but, every one is effected with manupulate method.
what can be the problem
thanks in advance

Not sure what you are asking.

Are you making copies of the objects the pointers in the ArrayList point to?
Or are there only one set of objects with differents sets of pointers to them.
The contents of the different arraylists all point to the same objects.
Changing them via one arraylist references will change them for all arraylist references

i want all the objects in the different arraylists have different instances.

for example

((ArrayList)encapsulatingArrayList.get(0)).get(1)

and

((ArrayList)encapsulatingArrayList.get(1)).get(1)

and

((ArrayList)encapsulatingArrayList.get(2)).get(1)

should be different instances but can not achive yet.
thanks for answer.

Then create new instances for each ArrayList. If you copy references from one arraylist to another, that doesn't create new instances.

i think i create new instances by:

tempList=new ArrayList<MyObj>();

do not i?

Here's a minimal block of code that shows different arraylists added to a master arraylist

ArrayList<ArrayList<String>> masterList = new ArrayList<ArrayList<String>>();
      ArrayList<String> temp = new ArrayList<String>();
      temp.add("A");
      temp.add("B");
      masterList.add(temp);
      temp = new ArrayList<String>();
      temp.add("C");
      temp.add("D");
      masterList.add(temp);
      System.out.println(masterList);

      String s = masterList.get(0).get(0);
      s += " etc";
      masterList.get(0).set(0, s);
      System.out.println(masterList);

Now how does your code differ from that, when you strip out all the other stuff?

i only changed string to myobj because of the need of details:

while(i<masterlist.size()){
manipulate(masterList.get(i));
i++;
}

i have also tried

ArrayList<MyObj> temp;
while(i<masterlist.size()){
temp=masterList.get(i);
manipulate(temp);
masterlist.set(i,temp);
i++;
}

after ONLY one iteration of loops above;

after one running of the lines:

manipulate(masterList.get(i));

or

manipulate(temp);

masterlist.get(0)
masterlist.get(1)
masterlist.get(2)

all have become same lists. all of them have "hello" in their attributeList member. HOW?

inside of MyObj:

class MyObj{
public ArrayList<String> attributeList=new Attribute<String>();
public int attributeInt=;
}

inside of manipulate

void manipulate(ArrayList<MyObj> slist){
slist.get(0).attributeList.add("hello"+slist.get(0).attributeInt);
}

thanks in advance

In your code it looks like you are iterating through all the ArrayLists applying the same maipulation to all of them, in which case you might expect the results to be the same.
You can check if the outer ArrayList has been populated with multiple refrences to the same inner ArrayList by printing masterlist.get(0)==masterlist.get(1)
If true you have populated the arraylist badly, if false you have multiple arraylists containing the same data. Either way it will help diagnosing.

hi
thanks very much for your attention
the lists have different values after population.
i debugged and saw this, i am sure they are different.
but
i found the right word to tell the problem

after "one and only one iteration" of the loop

ArrayList<MyObj> temp;
    while(i<masterlist.size()){
    temp=masterList.get(i);
    manipulate(temp);
    masterlist.set(i,temp);
    i++;
    }

all the little arraylists has become populated with hello.

Sorry, it's still not clear to me what you are saying. What happens if you don't execute that loop but just execute

temp=masterList.get(0);
manipulate(temp);
masterlist.set(0,temp);

does that also affect all the subLists?

yes, exactly.

OK
Now please try
masterlist.get(0)==masterlist.get(1)
before and after that code block

Do you have ANY static members (other than the main method)?

masterlist.get(0)==masterlist.get(1) returns false after the block

and
i am very sorry i must tell before but the method which has the problem block is static. manipulate method is static too.

any of the lists, sub or root are not static.

Still trying to see if the duplication is a case of duplicate references to the same object, or different objects with the same data...
how about
masterlist.get(0).get(0)==masterlist.get(1).get(0)
after running the manipulate?

masterlist.get(0).get(0)==masterlist.get(1).get(0)

returns true.

masterlist.get(0).get(0)==masterlist.get(1).get(0)

before manipulate false
after manipulate true

sorry sorry it returns both false before and after

edit:

i wrote true because i run the loop by mistake:

while(i<masterlist.size())
{
manipulate(masterlist.get(0))
i++;
}

after running with same iteration number for masterlist.size() time, the instances become the same.

OK, so that seems to prove that they are two different Objects, but you are updating both those elements to the same value. Either some variable somewhere is static or you are executing code repeatedly across the arraylists. I realise you think both of those are not happening, but beware, if your assumptions and understandings of the code were all correct, it would work. At least one think you believe about your code is wrong. You just have to find out which one!
I can't help any further unless you can post the entire code of a runnable program that displays the problem. I'm going to be busy for the next 2-3 hours, so see you later.

thanks very much, my variable names are all in turkish so i am afraid you dont want to struggle with it hope to find it with your ideas thanks a lot.

OK. Good Luck!
J

hi,
problem has been solved.

@JamesCherrill, you were right, the population was wrong, just not in sub arraylists, member lists of the arraylist element objects.

when i defining a new object with

new MyObj(myObj);

i was using the constructer

public MyObj(MyObj myObj){
this.memberlist=myObj.memberlist;
//the mistake was here 
//memberlist is another arraylist
...

}

i changed the code with

public MyObj(MyObj myObj){
this.memberlist=new ArrayList<AnotherObject>();
this.memberlist.addAll(myObj.memberlist);
...

}

in a forum it has been told to me to "deep copy" my arraylists
now i see, i must "deep copy" the "member lists" too.

thanks for all your time

That's great!
Many thanks for coming back with the solution; it's surprising how many people just vanish as soon as their problem is solved, so we never know how.
You are using a "copy constructor", and these can be a problem for even the most experienced programmer. If the class is a bit complex, especially if it contains Collections, it's hard to know when to deep copy the data vs when to copy the reference only.
I suggest it's time to mark this "solved" - you can start a new thread if you hit a new problem.
cheers
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.