Define an integer array ‘arr’ with size 2 in the main method and initialize the two elements in ‘arr’ to be 1 and 2
Write a method ‘swap’ and pass the ‘arr’ into the method, swap two elements of the ‘arr’ in the method
Print out all elements in ‘arr’ in the main method
System.out.print(arr[0] + ‘ ‘ + arr[1]);
Swap(arr);
System.out.print(arr[0] + ‘ ‘ + arr[1]);
public class ArrayInt
{
public static void main (String[] args)
{
int [] arr = new int [2];
arr [0] = 1;
arr [1] = 2;
//System.out.println(arr[0] + " " + arr[1]);
}
public void swap (int [] arr)
{
for (int tmp : arr)
{
arr [0] = arr[1];
arr[1] = tmp;
}
System.out.println(arr[0] + " " + arr[1]);
}
}