public class myProg
{
public static void main(String[] args)
{
double[] myList = {1, 2, 3, 4, 5, 6, 7, 8};
// Write a program to swap adjacent elements
for ( int i = 0; i < myList.length; i++ )
System.out.print( myList[i] + " " );
}
}
|
You are to swap array elements myList[i] with myList[i+1] for i = 0, 2, 4, 6
I.e.: swap these elements:
myList[0] <--> myList[1] myList[2] <--> myList[3] myList[4] <--> myList[5] myList[6] <--> myList[7] |
Your program is correct if you see this output:
2.0 1.0 4.0 3.0 6.0 5.0 8.0 7.0 |