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