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]

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]));
}
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.
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[][].
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?
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