You could do it more simply using ArrayLists or Arrays.
With Arrays, you create 3 int[]
and 1 int[][]
.
What this translates to is "3 arrays of int
s and 1 array of arrays of ints
". That is what #4 is: an array of your other arrays. In code it is:
int[]
arr1 = {1, 2, 3},
arr2 = {4, 5, 6},
arr3 = {7, 8, 9};
int[][] arr4 = {arr1, arr2, arr3};
Alternatively, you could use ArrayLists, which differ from Arrays in that you can add or remove elements, among many other actions. The logic is the same in this case, just the syntax is different. You create 3 ArrayList<Integer>
and 1 ArrayList<ArrayList<Integer>>
, which translates to "3 ArrayList
s of Integer
s and 1 ArrayList
of ArrayList
s of Integer
s." In code it is:
ArrayList<Integer>
list1 = new ArrayList<>(Arrays.asList(1, 2, 3)),
list2 = new ArrayList<>(Arrays.asList(4, 5, 6)),
list3 = new ArrayList<>(Arrays.asList(7, 8, 9));
List<ArrayList<Integer>> list4 = new ArrayList<>
(Arrays.asList(list1, list2, list3));
Finally, you can print the output of both methods:
System.out.println("Arrays - int[][]: " + Arrays.deepToString(arr4)
+ "\nLists - List<ArrayList<Integer>>: " + list4);
And you will get:
Arrays - int[][]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Lists - List<List<Integer>>: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Does this help?