// the program swaps the first and the last elements

public class Swap  {

    public static void main(String [] args) {
	int [] a = {1, 2, 3, 4, 5};

	int temp = a[0];
	a[0] = a[a.length - 1];
	a[a.length - 1] = temp;

	for (int i = 0; i < a.length; ++i) {
	    System.out.println("a[" + i  + "] = " + a[i]);
	}
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.