Java – ArrayList Operations Menu Driven Approach

Following program explains how to perform ArrayList operations such as Append, Insert, Replace, Update, Remove, Sort, Reverse and Display:

import java.util.*;
class Main
{
            public static void main(String[] args)
            {
                        List<Integer> list = new ArrayList<Integer>();
                        Scanner sc = new Scanner(System.in);
                        while(true)
                        {
                                    System.out.println(“1.Append \n2.Insert \n3.Replace \n4.Remove \n5.Display \n6.Sort \n7.Reverse \n8.Quit”);
 
                                    System.out.print(“Enter choice : “);
                                    int ch = sc.nextInt();
                                    if(ch==1)
                                    {
                                                System.out.print(“Enter element to append : “);
                                                int ele = sc.nextInt();
                                                list.add(ele);
                                                System.out.println(“Element added”);
                                    }
                                    else if(ch==2)
                                    {
                                                System.out.print(“Enter index : “);
                                                int index = sc.nextInt();
 
                                                if(index>=0 && index<=list.size()-1)
                                                {
                                                            System.out.print(“Enter element : “);
                                                            int ele = sc.nextInt();
                                                            list.add(index, ele);
                                                            System.out.println(“Element inserted”);
                                                }
                                                else
                                                            System.out.println(“No such location”);
                                    }
                                    else if(ch==3)
                                    {
                                                System.out.print(“Enter element to replace : “);
                                                int ele = sc.nextInt();
 
                                                if(list.contains(ele))
                                                {
                                                            int index = list.indexOf(ele);
                                                            System.out.print(“Enter new element : “);
                                                            int x = sc.nextInt();
                                                            list.set(index, x);
                                                            System.out.println(“Element replaced”);
                                                }
                                                else
                                                            System.out.println(“No such element in list”);
                                    }
                                    else if(ch==4)
                                    {
                                                System.out.print(“Enter element to remove : “);
                                                int ele = sc.nextInt();
                                                if(list.contains(ele))
                                                {
                                                            int index = list.indexOf(ele);
                                                            list.remove(index);
                                                            System.out.println(“Element removed”);
                                                }
                                                else
                                                            System.out.println(“No such element to remove”);
                                    }
                                    else if(ch==5)
                                    {
                                                if(list.isEmpty())
                                                            System.out.println(“Empty list”);
                                                else
                                                            System.out.println(“List is : ” + list);
                                    }
                                    else if(ch==6)
                                    {
                                                Collections.sort(list);
                                                System.out.println(“List sorted”);
                                    }
                                    else if(ch==7)
                                    {
                                                Collections.reverse(list);
                                                System.out.println(“List reversed”);
                                    }
                                    else if(ch==8)
                                    {
                                                System.out.println(“End”);
                                                System.exit(1);
                                    }
                                    else
                                                System.out.println(“Invalid choice”);
                        }
            }
}
Scroll to Top