EvilZone

Programming and Scripting => Java => : peak August 17, 2012, 07:35:28 AM

: strange problems with arraylists ...
: peak August 17, 2012, 07:35:28 AM
Look at the output. its a shorter version . The time in the output is not the same. The object printed after "dafuck..." are all the same. but before "dafuck.." are diffrent with every loop.

why is this so? I don't get it ...

:
import java.util.ArrayList;
import java.util.Iterator;

public class test {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        ArrayList<ArrayList<String[]>> inputList = new ArrayList<ArrayList<String[]>>();
        ArrayList<String[]> tagList = new ArrayList<String[]>();
               
        inputList.clear();
        for(int i=0;i<3;i++)
        {           
            tagList.clear();
            for(int y=0;y<2;y++)
            {
                String[] test = new String[2];               
                test[0] = "First  " + y + " " + System.currentTimeMillis();
                System.out.println(test[0]);
                Thread.sleep(200);               
                test[1] = "second " + y + " " + System.currentTimeMillis();
                System.out.println(test[1]);
                Thread.sleep(200);
                tagList.add(test);
            }
            inputList.add(tagList);
           
            System.out.println(inputList.get(i)); //each time another object
        }
       
        System.out.println("dafuck is this ... ?");
        System.out.println(inputList.get(0)); //this is the same object
        System.out.println(inputList.get(1)); //as this and
        System.out.println(inputList.get(2)); //as this.

        }
}

: Re: strange problems with arraylists ...
: Deque August 17, 2012, 09:22:13 AM
You put the same object into inputList three times.
The reason is that you only clear tagList, which means you keep the same instance, but remove all its contents. You do not assign a new instance to tagList in the loop. It is the same all the time.

The toString method shows the object's classname and hash as default. Since you change tagList in the loop, the hash has to change too. It doesn't mean that you have another instance than last time.

Maybe it helps to understand if you try this:

:
public static void main(String[] args) throws InterruptedException {
        System.out.println("test");
        ArrayList<ArrayList<String[]>> inputList = new ArrayList<ArrayList<String[]>>();
        ArrayList<String[]> tagList = new ArrayList<String[]>();

        inputList.clear();
        for (int i = 0; i < 3; i++) {
            tagList.clear();
            for (int y = 0; y < 2; y++) {
                String[] test = new String[2];
                test[0] = "First  " + y + " " + System.currentTimeMillis();
                System.out.println(test[0]);
                Thread.sleep(200);
                test[1] = "second " + y + " " + System.currentTimeMillis();
                System.out.println(test[1]);
                Thread.sleep(200);
                tagList.add(test);
            }
            inputList.add(tagList);

            for (int j = 0; j <= i; j++)
                System.out.println(inputList.get(j)); // each time another
                                                        // object
        }

        System.out.println("dafuck is this ... ?");
        System.out.println(inputList.get(0)); // this is the same object
        System.out.println(inputList.get(1)); // as this and
        System.out.println(inputList.get(2)); // as this.

    }

Afterwards change tagList.clear(); to tagList = new ArrayList<String[]>(); and try again.
: Re: strange problems with arraylists ...
: peak August 17, 2012, 11:09:16 AM
awww i see now. I actually knew that but somehow forgot it. Thanks alot!! Now I can finally go on with my project :)