Skip to main content

2D Arrays

Printing 2D Arrays

info

Best & Fastest Method: Using for-Each Loop

for (int[] el:newArr) {
for (int el2:el) {
System.out.print(el2 + " ");
}
System.out.println();
}

Copying a 2D Array to another 2D Array

info
int[][] arr = {{2, 3, 54, 56}, {5, 4, 324, 23}, {23, 344, 45, 33}, {23, 34, 11, 3}};    // a 2D array
int[][] newArr = new int[arr.length][arr[0].length];

for (int i=0; i<arr.length; i++) { // range is the ROW
newArr[i] = Arrays.copyOf(arr[i], arr[i].length); // copies all COLUMNS of ROW "i" of arr[i] to all COLUMNS of ROW "i" of newArr[i]
}