need to add 2d double array to arraylist

How do I add a two dimensional double array to an arraylist?
[67 byte] By [vik_paa] at [2007-9-30 2:24:37]
# 1

One way one doing this:

double[][] array2d = { { 1., 3. }, { 2., 4. }, { 6., 5. } };

ArrayList list = new ArrayList();

for (int i = 0; i < array2d.length; i++)

{

for (int j = 0; j < array2d[i].length; j++)

list.add(new Double(array2d[i][j]));

}

jfbrierea at 2007-7-16 13:34:08 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
i just done array2d.toString(). is that ok? now i want to know how get the object from the arraylist and convert back to double array?
vik_paa at 2007-7-16 13:34:08 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
i think your code adds each double within the array to a different posn within the arraylist. right? i wanted to add the whole array to 1 posn i.e keep the form of the array. i guess i could add that arraylist you created and add it to another arraylist, but then thats an extra step.
vik_paa at 2007-7-16 13:34:08 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
your_arraylist.add(your_array);To get it back...doube[][] my_array =(double[][]) your_arraylist.get(it);toString() won't work. toString() returns a String and that's what you will add to the array list. When you get() it, it will be a String, not a double[][].
atmguya at 2007-7-16 13:34:08 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
thanks atmguy, it works great. i dont understand how i can add a double array to an arraylist directly like that. it never works when i try to add a single integer or double value? i thought you could only add elements of type object?
vik_paa at 2007-7-16 13:34:08 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6

There are two kinds of types in the Java programming language: primitive types and reference types.

A primitive type is predefined by the Java programming language and named by its reserved keyword:

boolean, byte, short, int, long, char, float or double.

There are three kinds of reference types: class types, interface types, and array types.

An object is a class instance or an array.

You can only pass object references to List.add(Object) method.

So:

double[] doubleArray = { 0. };

myList.add(doubleArray) is ok

And:

Double myDoubleObject = new Double(0.);

myList.add(myDoubleObject) is ok

But:

double myDouble = 0.;

myList.add(myDouble) is NOT ok

jfbrierea at 2007-7-16 13:34:08 > top of Java-index,Archived Forums,New To Java Technology Archive...