I have a 2D Double array that is expanding and I am not sure of its final size therefore I want to convert it to a 2d arraylist but not sure how to do this I tried to define the array list first and use addAll(Array) method but it doesn't work. Any suggestion?
I just wrote this code I think it will work fine but I am looking for more efficient way
public class trial {
public static void main(String[] arg){
double[][] Solutions_to_arrange={{1,5,2,8,4,70,50,80},{3,7,4,2,6,60,30,70},{8,1,6,4,2,10,40,60}};
ArrayList<ArrayList<Double>> Solutions_to_arrange_A=new ArrayList<ArrayList<Double>>();
for(int i=0;i<Solutions_to_arrange.length;i++){
Solutions_to_arrange_A.add(new ArrayList<Double>());
for(int j=0;j<Solutions_to_arrange[0].length;j++){
Solutions_to_arrange_A.get(i).add(Solutions_to_arrange[i][j]);
}
}
}
}
Also I have question on putting the statement
Solutions_to_arrange_A.add(new ArrayList<Double>());
Should I put it within the loop or its sufficient to call it just once outside the loop
ArrayList<ArrayList<Double>>, or anArrayList<Double[]>, or something else?double[][]and notDouble[][], there's really no way to do any better, that I know of. If you make itDouble[][], you'll have to put a.0after every integer, since Java doesn't like converting an unboxedintto a boxedDoubleautomatically. But if you do both of these, you can write code with a single loop usingarrays.asList. But this only works in one dimension, so you'd still need one loop.Dafter every integer)