public static void main(String[] args) {
int[] array1 = {1,2,3};
int[] array2 = {4,5,6};
System.out.println("Array1 = " + Arrays.toString(array1));
System.out.println("Array2 = " + Arrays.toString(array2));
swapAll(array1, array2);
System.out.println("After swaping Array1 and Array2:");
System.out.println("Array1 = " + Arrays.toString(array1));
System.out.println("Array2 = " + Arrays.toString(array2));
}
public static void swapAll(int[]array1, int[]array2) {
for(int i=0; i<array1.length; i++) {
int temp = array1[i];
array1[i] = array2[i];
array2[i] = temp;
}
}
}
//output: Array1 = [1, 2, 3]
Array2 = [4, 5, 6]
After swaping Array1 and Array2:
Array1 = [4, 5, 6]
Array2 = [1, 2, 3]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter