It is possible, with an ArrayList<ArrayList<Integer>>
, or any other type than Integer.
Essentially when you look at a multi-dimensional array you have an array of arrays of a certain type. In this case, to populate the array or print 3x3 ArrayList, you would iterate through them like you would with an array:
Given an ArrayList as such: ArrayList<ArrayList<Integer>> box
for (int i = 0; i < box.size(); i++) {
for (int j = 0; j < box.get(i).size(); j++) {
System.out.print(box.get(i).get(j));
}
System.out.println();
}
This is a very simple example. You can also use forEach loops as such:
for (ArrayList<Integer> row: box) {
for (Integer cell: row) {
System.out.print(cell);
}
System.out.println();
}
Note that initializing the list is a bit trickier with multiple "dimensions":
for (int i = 0; i < 3; i++) {
box.add(new ArrayList<Integer>());
for (int j = 0; j < 3; j++) {
box.get(i).add(...);
}
}
Keep in mind that this answer uses very simple Java, there is definitely more graceful ways to do it. However, I suspect that this is probably for a homework, hence the type of answer given.