You are on page 1of 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.3
Student Name: Kaustabh Pal UID: 21BCS8791
Branch: CSE Section/Group: SC-905(B)
Semester: 6th Date of Performance: 29-02-24
Subject Name: JAVA LAB Subject Code: 21CSH-319

1. Aim: Write a Program to perform the basic operations like insert, delete,
display and search in list. List contains String object items where these
operations are to be performed.

2. Objective: To learn about concept of Array List, to learn about various


methods of List.

3. Input/Apparatus:
Hardware Requirements:- Minimum 384MB RAM, 100 GB hard disk,
processor with 2.1 MHz

Software Requirements:- Eclipse, NetBeans, IntelliJ, etc.

4. Procedure:
import java.util.*;

public class ListOperations {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

List<String> stringList = new ArrayList<>();

while (true) {

System.out.println("Choose an operation:");

System.out.println("1. Insert String");

System.out.println("2. Delete String");

System.out.println("3. Display all elements");


System.out.println("4. Search String");

System.out.println("5. Exit");

int choice = scanner.nextInt();

scanner.nextLine();

switch (choice) {

case 1:

System.out.println("Enter a string to insert:");

String insertString = scanner.nextLine();

stringList.add(insertString);

System.out.println("String inserted successfully.");

break;

case 2:

System.out.println("Enter the index of the string to delete:");

int index = scanner.nextInt();

scanner.nextLine();

if (index >= 0 && index < stringList.size()) {

String deletedString = stringList.remove(index);

System.out.println("String '" + deletedString + "' deleted


successfully.");

} else {

System.out.println("Invalid index.");

break;

case 3:

if (stringList.isEmpty()) {

System.out.println("List is empty.");

} else {
System.out.println("All elements in the list:");

for (String str : stringList) {

System.out.println(str);

break;

case 4:

System.out.println("Enter the string to search:");

String searchString = scanner.nextLine();

if (stringList.contains(searchString)) {

System.out.println("String '" + searchString + "' found in the list.");

} else {

System.out.println("String '" + searchString + "' not found in the


list.");

break;

case 5:

System.out.println("Exiting...");

scanner.close();

System.exit(0);

default:

System.out.println("Invalid choice. Please choose a number from 1 to


5.");

}
5. Output:

You might also like