What is wrong with this implimentation of the .add() method?

Here is a simplified version of the primary bug in a much larger hunk of code. If you run this code you will notice that ONLY the 20th iteration of the array1 data gets added to the ArrayList. WHY?! I need all the unique arrays added. Please help.

import java.util.ArrayList;

import java.util.Random;

publicclass listBuilder{

publicstaticvoid main(String args[]){

ArrayList<long[]> uArrayList =new ArrayList<long[]>();

int N = 7, L = 20;

long[] array1 =newlong[N];

long[] array2 =newlong[N];

Random why =new Random();

for (int i=0; i < L; i++){

for (int j = 0; j < N; j++){

array1[j] = (long)why.nextInt(10);

}

uArrayList.add(array1);

}

for (int i = 0; i < L; i++){

System.out.print(i +": ");

array2 = (long[])uArrayList.get(i);

for (int j = 0; j < N; j++){

System.out.print(array2[j] +" ");

}

System.out.println();

}

}

}

[2308 byte] By [Fontaignea] at [2007-11-26 23:39:23]
# 1
Because you always add the same array object.
stefan.schulza at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 2
hmm... maybe you could be more specific about your answer. Just a thought.
Fontaignea at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 3

You allocated array1 once.

You set array1[0..n] to some value,

then add array1 to your collection.

You then change array1[0..n] to some other value,

then add array1 AGAIN to your collection.

So your collection always contains just 1 array,

and the array will only contain the most recent values

(since each iteration of the loop you were erasing old values)

KathyMcDonnella at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 4

Try this:

public class listBuilder {

public static void main(String args[]){

ArrayList<long[]> uArrayList = new ArrayList<long[]>();

int N = 7, L = 20;

Random why = new Random();

for (int i=0; i < L; i++){

long[] array1 = new long[N];

for (int j = 0; j < N; j++){

array1[j] = (long)why.nextInt(10);

}

uArrayList.add(array1);

}

for (int i = 0; i < L; i++){

System.out.print(i + ": ");

long[] array2 = (long[])uArrayList.get(i);

for (int j = 0; j < N; j++){

System.out.print(array2[j] + " ");

}

System.out.println();

}

}

}

KathyMcDonnella at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 5
Thank you very much.
Fontaignea at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 6

> So your collection always contains just 1 array,

> and the array will only contain the most recent

> values

> (since each iteration of the loop you were erasing

> old values)

Well, not exactly. the collection will contain 20 times the same array object, as it is a list not a set.

stefan.schulza at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 7

> > So your collection always contains just 1 array,

> > and the array will only contain the most recent

> > values

> > (since each iteration of the loop you were erasing

> > old values)

>

> Well, not exactly. the collection will contain 20

> times the same array object, as it is a list not a

> set.

Right. Quite right. I meant the number of distinct Array in his collection is 1.

KathyMcDonnella at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 8
Sorry for nitpicking :)
stefan.schulza at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...
# 9
Oh, not at all. :)I believe in exactness. :)
KathyMcDonnella at 2007-7-11 15:04:53 > top of Java-index,Core,Core APIs...