Skip to main content

Arrays Functions

copyOf()

info

Normal Array

int[] ar1 = {3, 4, 45, 565, 56, 7};
int[] ar2 = new int[ar1.length];

ar2 = Arrays.copyOf(ar1, ar1.length); //copies ar1 to ar2


2D Array

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]
}