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