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

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


Program:
import java.io.*;
import java.util.*;

class ReverseArrayList {
    public static void main(String args[]) throws IOException {
        ArrayList<String> strList = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int i,n;
        String str;
        System.out.println("Enter how many strings:");
        n=Integer.parseInt(br.readLine());
        for(i=0;i<n;i++){
            System.out.println("Enter string:");
            str = br.readLine();
            strList.add(str);
        }
        Collections.reverse(strList);
        System.out.println("Reverse Order List:"+strList);
    }
}


Post a Comment

Previous Post Next Post