1

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

1 Answer 1

2

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]);
       }        
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.