Author Topic: strange problems with arraylists ...  (Read 1842 times)

0 Members and 1 Guest are viewing this topic.

Offline peak

  • Serf
  • *
  • Posts: 26
  • Cookies: 0
    • View Profile
strange problems with arraylists ...
« on: 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 ...

Code: [Select]
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.

        }
}


Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: strange problems with arraylists ...
« Reply #1 on: 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:

Code: [Select]
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.

Offline peak

  • Serf
  • *
  • Posts: 26
  • Cookies: 0
    • View Profile
Re: strange problems with arraylists ...
« Reply #2 on: 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 :)