1 /* 2 * Program that works with ArrayList. 3 */ 4 5 package arraylistplay; 6 import java.util.ArrayList; 7 8 public class Primes { 9 10 public static void main(String[] args) { 11 12 ArrayList<Integer> primes = new ArrayList<>(); 13 14 primes.add(2); 15 primes.add(3); 16 primes.add(5); 17 primes.add(7); 18 19 System.out.println("size of primes list = " + primes.size()); 20 System.out.println("first prime = " + primes.get(0)); 21 System.out.println("last prime = " + primes.get(3)); 22 System.out.println("last prime = " + primes.get(primes.size()-1)); 23 24 System.out.println("\nThe initial list..."); 25 for ( Integer prime : primes ) { 26 System.out.println(prime); 27 } 28 29 int temp = primes.get(0); 30 primes.set(0, primes.get(primes.size()-1)); 31 primes.set(primes.size()-1,temp); 32 33 System.out.println("\nThe final list..."); 34 for ( Integer prime : primes ) { 35 System.out.println(prime); 36 } 37 } 38 } 39