0% found this document useful (0 votes)
2 views

Menu e Driven Program

This Java program implements a menu-driven interface for managing an ArrayList of integers. Users can add, sort, replace, remove, display elements, and insert an element between two existing elements. The program continues to prompt the user for actions until they choose to exit.

Uploaded by

220902
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Menu e Driven Program

This Java program implements a menu-driven interface for managing an ArrayList of integers. Users can add, sort, replace, remove, display elements, and insert an element between two existing elements. The program continues to prompt the user for actions until they choose to exit.

Uploaded by

220902
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/* Menu driven Program to create an ArrayList and perform the

operations i)Adding elements ii)Sorting elements iii)Replace an


element with another iv)Removing an element V)Displaying all the
elements vi) Adding an element between 2 elements*/

import java.util.*;
import java.io.*;
public class MenuDriven4 {

public static void main(String[] args) throws IOException{


Scanner scan= new Scanner(System.in);
ArrayList<Integer>arrayList = new ArrayList<>();
int ch;
do{
System.out.println("1.Add Element");
System.out.println("2.Sort Element");
System.out.println("3.Replace an Element");
System.out.println("4.Remove an Element");
System.out.println("5.Display all Elements");
System.out.println("6.Add an element between two element");
System.out.println("7.Exit");
System.out.println("Enter your choice");
ch=scan.nextInt();
switch(ch)
{
case 1: System.out.println("enter element to add:");
int eletoadd = scan.nextInt();
arrayList.add(eletoadd);
System.out.println("element added successfully");
break;
case 2 :Collections.sort(arrayList);
System.out.println("elements sorted succesfully");
break;
case 3: System.out.println("Enter index to replace");
int indexToReplace = scan.nextInt();

if(indexToReplace>=0&&indexToReplace<arrayList.size())
{
System.out.println("Enter new element");
int newElement =scan.nextInt();
arrayList.set(indexToReplace,newElement);
System.out.println("element replaced
successfully");
}
else
{
System.out.println("Invalid Index");
}
break;
case 4: System.out.println("Enter index to remove");
int indexToRemove=scan.nextInt();
if(indexToRemove>=0 &&
indexToRemove<arrayList.size())
{
arrayList.remove(indexToRemove);
System.out.println("Elements removed
successfully");
}
else
{
System.out.println("Invalid Index");
}
break;
case 5: System.out.println("All elements");
for(int element:arrayList)
{
System.out.println(element +"");
}
System.out.println("");
break;
case 6: System.out.println("Enter index to insert
after:");
int indexToInsertAfter=scan.nextInt();

if(indexToInsertAfter>=0&&indexToInsertAfter<arrayList.size())
{
System.out.println("Enter the element to
insert:");
int elementToInsert =scan.nextInt();

arrayList.add(indexToInsertAfter+1,elementToInsert);
System.out.println("Element added
Successfully");
}
else
{
System.out.println("Invald index");
}
break;
case 7: System.out.println("Exiting....");
break;
default:System.out.println("Invalid Choice,Please Enter
a number between 1 and 7");
}
} while(ch!=7);
scan.close();
}

You might also like