1. Implement the following method:
public static void swapHalves(int[] array)
The method must modify the input array by swapping its first and second halves. The values in each half must maintain their original order. If an array has an odd length, the central value must remain unchanged in its position.
Examples:
array (before) array (after)
{ 2, 3, 4, 7, 8, 9 } { 7, 8, 9, 2, 3, 4 }
{ 4, 2, 6, 5, 3, 3, 9 } { 3, 3, 9, 5, 4, 2, 6 }
package javaapplication1; public class JavaApplication1 { public static void main(String[] args) { int a[]= new int[6]; a[0]=1; a[1]=3; a[2]=7; a[3]=12; a[4]=9; a[5]=11; int c[]=new int[a.length]; StampaArray(a); System.out.println(); c=ScambiaMetà(a); System.out.println(); StampaArray(c); } public static int[] ScambiaMetà(int[] b){ int risultato[]=new int [b.length]; int lunghezza; lunghezza=b.length; System.out.print(lunghezza); int k=0; // lavora sulla prima meta del vettore for(int n=0; n<(lunghezza/2); n++) {risultato[lunghezza/2 + k]=b[n]; k++; } k=0; //lavora sulla seconda metà for(int i=lunghezza/2; i<lunghezza; i++) {risultato[k]=b[i]; k++; } return risultato; } public static void StampaArray (int[]v){ for(int i=0; i<v.length; i++){ System.out.print(v[i]+";"); } } }