Write a JAVA program to read n strings & insert into ArrayList collection. Display the elements of collection in reverse order.

import java.util.*;
import java.io.*;
class Slip27
{
    public static void main(String ar[])throws IOException
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\t Enter size of arraylist ");
        n=Integer.parseInt(br.readLine());
        ArrayList <String> a = new ArrayList<String>(n);
        for(int i=0;i<n;i++)
        {
            System.out.println("Enter element");
            a.add(br.readLine());
        }
        System.out.println(" The contents of arraylist using iterator..");
        Iterator <String> it = a.iterator();
        while(it.hasNext())
           System.out.println(" \t  " + it.next());
        ListIterator <String> lit = a.listIterator();
           while(lit.hasNext())
         lit.next();
           System.out.println("The contents of arraylist in reverse order... ");
          while(lit.hasPrevious())
           System.out.println("  \t  " + lit.previous());
    }
    }

1 comment: