I have class Box, which describes parameters of box (height, width, length):
public class Box {
public int h;
public int w;
public int l;
public Box(int h, int w, int l) {
this.h = h;
this.w = w;
this.l = l;
}
In input i have list of boxes, how to convert it to two dimensional array? One row = one box:
List<Box> listBox1 = new ArrayList<>();
listBox1.add(new Box(12, 11, 12));
listBox1.add(new Box(11, 12, 12));
listBox1.add(new Box(1, 1, 1));
listBox1.add(new Box(67, 34, 13));
Output:
12 11 12 // 1st box and e.t.c...
11 12 12
1 1 1
67 34 13
CodePudding user response:
Your two dimensional array should of size (m * n) where m is length of box list and n is fixed 3 for three properties (h, w, l).
Get the length of arrayList int length = listBox1.size();
A sample code as below.
int[][] array = new int[length][3];
for(Box box : listBox1){
int index = listBox1.indexOf(box);
array[index][0] = box.h;
array[index][1] = box.w;
array[index][2] = box.l;
}
for(int i=0 ; i<array.length; i ){
System.out.println(array[i][0] " " array[i][1] " " array[i][2]);
}
