1 Write a method that takes two arrays of integers as param
1. Write a method that takes two arrays (of integers ) as parameters. The method should swap all of the values in both arrays. Assume that the arrays will be the same size and not empty.
2.Write a method that prints all the values in a 2-dimensionaWrite a method that prints all the values in a 2-dimensional array that it receives as a parameter. Assume that the array is an array of integersl array that it receives as a parameter. Assume that the array is an array of integers
Solution
TestArray.java
import java.util.Arrays;
public class TestArray {
public static void main(String[] args) {
int a[]= {1,2,3,4,5};
int b[] = {5,4,3,2,1};
swapArray(a,b);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
int c[][] = {{1,2,3,4,5},{2,3,4,5,6}};
printArray(c);
}
public static void swapArray(int a[], int b[]){
int temp =0;
for(int i=0; i<a.length; i++){
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
public static void printArray(int a[][]){
for(int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++){
System.out.print(a[i][j]+\" \");
}
System.out.println();
}
}
}
Output:
[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]
1 2 3 4 5
2 3 4 5 6
