Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to create an array list of string arrays. When I am done, I want the array list to look like this:

[0,0], [0,1], [1,0], [1,1,]

I have tried to define an array and then add it to the array list. Then re-define an array and add it again. But the array list only seems to contains the last entry. Take a look:

String[] t2 = new String[2]; 

ArrayList<String[]> list2 = new ArrayList<String[]>();

t2[0]="0";
t2[1]="0";
list2.add(t2);
t2[0]="0";
t2[1]="1";
list2.add(t2);
t2[0]="1";
t2[1]="0";
list2.add(t2);
t2[0]="1";
t2[1]="1";
list2.add(t2);

for (String[] tt : list2) 
{
System.out.print("[");
for (String s : tt)
System.out.print(s+" ");
System.out.print("]");
}

The output is:

[1,1] [1,1] [1,1] [1,1]

Any idea on how to add each array to my array list?`

share|improve this question
    
people have already answered your questions, just a comment why dont you do this? list2.add(new String[] {"1","0"}); –  Aadi Droid Aug 16 '13 at 16:10

5 Answers 5

up vote 2 down vote accepted

The problem is that you are adding the same object to each index of your ArrayList. Every time you modify it, you are modifying the same object. To solve the problem, you have to pass references to different objects.

String[] t2 = new String[2]; 

ArrayList<String[]> list2 = new ArrayList<String[]>();

t2[0]="0";
t2[1]="0";
list2.add(t2); 

t2 = new String[2]; // create a new array
t2[0]="0";
t2[1]="1";
list2.add(t2);

t2 = new String[2];
t2[0]="1";
t2[1]="0";
list2.add(t2);

t2 = new String[2];
t2[0]="1";
t2[1]="1";
list2.add(t2);
share|improve this answer

You are adding the same array t2 over and over. You need to add distinct arrays.

ArrayList<String[]> list2 = new ArrayList<String[]>();

list2.add(new String[] {"0", "0"});
list2.add(new String[] {"0", "1"});
list2.add(new String[] {"1", "0"});
list2.add(new String[] {"1", "1"});

As an aside: you can use Arrays.toString(tt) inside the loop to format each String[] the way you are doing.

share|improve this answer

This is because you're adding t2 to all of the entries. Each time you overwrite t2 you're changing all the values. This is because t2 isn't being passed by value, it is being passed by reference and saved by reference.

share|improve this answer

You are only creating one array, and putting its reference repeatedly into the ArrayList.

Instead, you can create a new array for each element in the List:

 ArrayList<String[]> list2 = new ArrayList<String[]>();

 String[] t2; 

 t2 = new String[2]; 
 t2[0]="0";
 t2[1]="0";
 list2.add(t2);

 t2 = new String[2]; 
 t2[0]="0";
 t2[1]="1";
 list2.add(t2);

 t2 = new String[2]; 
 t2[0]="1";
 t2[1]="0";

 t2 = new String[2]; 
 list2.add(t2);
 t2[0]="1";
 t2[1]="1";
 list2.add(t2);
share|improve this answer

You have just one item (t2) and each time you add it to the ArrayList . actually the reference of t2 is going to be saved in ArrayList so when you change the t2 value all of references in the ArrayList take effect.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.