? ArrayList<ArrayList><Double>> to double[][], and similarly for Integer

Dear all, I tried a lot and didn't get an elegant solution to convert ArrayList<ArrayList><T>> to T[][], where T in my mind is Integer or Double. Any suggestions?I
[193 byte] By [patioabrbecuea] at [2007-10-3 4:33:15]
# 1
What have you tried? Have you looked at toArray()?
zadoka at 2007-7-14 22:36:49 > top of Java-index,Core,Core APIs...
# 2

> Dear all, I tried a lot and didn't get an elegant

> solution to convert ArrayList<ArrayList><T>> to T[][],

> where T in my mind is Integer or Double. Any

> suggestions?

>

> I

which less elegant solution have you got so far?

r035198xa at 2007-7-14 22:36:49 > top of Java-index,Core,Core APIs...
# 3
plz explain the problem clearly
haishaia at 2007-7-14 22:36:49 > top of Java-index,Core,Core APIs...
# 4

I created a code that return a matrix with a Collection of Collections of a type.

public static <E> E[][] asMatrix(Collection<? extends Collection<E>> collection){

E[][] matrix=(E[][])new Object[collection.size()][];

int i=0;

for(Collection<E> subCollection:collection)

matrix[i++]=(E[])subCollection.toArray();

return matrix;

}

.

You can use this or create an method for each basic type, example:

public static int[][] asIntMatrix(Collection<? extends Collection<? extends Number>> collection){

int[][] matrix=new int[collection.size()][];

int i=0;

for(Collection<? extends Number> subCollection:collection) {

int [] column=new int[subCollection.size()];

int j=0;

for(Number number:subCollection)

matrix[i][j++]=number.intValue();

i++;

}

return matrix;

}

.

govisagod512a at 2007-7-14 22:36:49 > top of Java-index,Core,Core APIs...
# 5

Here's a belated direct approach:

ArrayList<ArrayList><Integer>> foo;

...

Integer myArray[][] = new Integer[foo.size()][];

for (int i = 0; i < foo.size(); i++) {

myArray[i] = foo.get(i).toArray( new Integer[0]);

}

sleepingwanderera at 2007-7-14 22:36:49 > top of Java-index,Core,Core APIs...