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