views:

196

answers:

3

I found some problem in creating dynamic 2d array with arraylist,The original code is tediously long to read so i am giving a simple code here,the problem is same in both the cases:

import java.util.*;

class test{
 public static void main(String args[]){
    Integer test[]=new Integer[3];

    ArrayList<Integer[]> al=new ArrayList<Integer[]>();

    int i,t;

     test[0]=1;
     test[1]=2;
     test[2]=3;


     al.add(test);
     test[0]=4;
     test[1]=5;
     test[2]=6;

  al.add(test);

     test[0]=7;
     test[1]=8;
     test[2]=9;

  al.add(test);



     test[0]=10;
     test[1]=11;
     test[2]=12;
  al.add(test);



     Integer table[][]=new Integer[al.size()][];
     table=al.toArray(table);

     for(i=0;i<=al.size()-1;i++){

     for(t=0;t<3;t++){
       System.out.print(" "+i+" "+t+" ");
       System.out.print(" "+table[i][t]+" ");}
     System.out.println();
 }   

   }
}

Output:

 0 0  10  0 1  11  0 2  12
  1 0  10  1 1  11  1 2  12
 2 0  10  2 1  11  2 2  12
 3 0  10  3 1  11  3 2  12

Output expected is

 0 0   1  0 1  2  0 2  3
 1 0   4  1 1  5  1 2  6
 2 0   7  2 1  8  2 2  9
 3 0  10  3 1  11  3 2  12

I don't under stand why the last element is over writting all other elements.

+3  A: 

Initialize a new Integer[] every time you add a new row.

That is, do it like this:

Integer[] test = new Integer[3];
List<Integer[]> al = new ArrayList<Integer[]>();
int i,t;
test[0]=1;
test[1]=2;
test[2]=3;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=4;
test[1]=5;
test[2]=6;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=7;
test[1]=8;
test[2]=9;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=10;
test[1]=11;
test[2]=12;
al.add(test);

Or better yet, do it this way:

List<Integer[]> al = new ArrayList<Integer[]>();
al.add(new Integer[]{1, 2, 3});
al.add(new Integer[]{4, 5, 6});
al.add(new Integer[]{7, 8, 9});
al.add(new Integer[]{10, 11, 12});
missingfaktor
A: 

Arrays are objects. ArrayList.add(E) adds a reference to the given E object to the list; it doesn't copy the object itself.

So you should do:

al.add(test);
test = new int[3];

Which creates a new array object, so the second row of data is written into a separate array.

meriton
A: 

Ahem,

In Java, everything is passed by reference (except for primitive types).

You're when you're doing:

 test[0]=1;
 test[1]=2;
 test[2]=3;
 al.add(test);
 test[0]=4;
 test[1]=5;
 test[2]=6;
 al.add(test);
 ...

A reference to the test array is passed in the list. You still have this reference to the array, and you modify it (while also being in the list)

Change that to :

 test[0]=1;
 test[1]=2;
 test[2]=3;
 al.add(test);

 test = new Integer[3]
 test[0]=4;
 test[1]=5;
 test[2]=6;
 al.add(test);
 ...
BenoitParis
"is passed by reference" is somewhat misleading, because that phrase has a different meaning in C# or C++. There, it means that assigning an argument variable in a method will write to the variable the caller used when invoking the method. In the terminology of those languages, Java passes everything by value, and the value is a reference if the expression is of reference type.
meriton
-1 - *"In Java, everything is passed by reference (except for primitive types)"* is simply incorrect.
Stephen C