Transfer a Two-Dimensional array to Two-Dimensional ArrayList?
Case 1 It is short, but need to covert the primitive type to reference type (int
to Integer
) as needed for Arrays.asList();
Integer[][] pattern = new Integer[][]{
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 2, 0, 0, 0, 2, 1 },
{ 1, 0, 3, 0, 3, 0, 1 },
{ 1, 0, 0, 4, 0, 0, 1 },
{ 1, 0, 3, 0, 3, 0, 1 },
{ 1, 2, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
};
List<List<Integer>> lists = new ArrayList<>();
for (Integer[] ints : pattern) {
lists.add(Arrays.asList(ints));
}
Case 2 If you don't want to covert the primitive type to reference type: (int[][] pattern = new int[][]
to Integer[][] pattern = new Integer[][]
)
List<List<Integer>> lists = new ArrayList<>();
for (int[] ints : pattern) {
List<Integer> list = new ArrayList<>();
for (int i : ints) {
list.add(i);
}
lists.add(list);
}