0% found this document useful (0 votes)
10 views6 pages

Java2 3

Uploaded by

Rohin Raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Java2 3

Uploaded by

Rohin Raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-2.3
Student Name: Rohin Raj Singh UID: 21BCS9791
Branch: BE-CSE Section: 626-B
Subject Name: Java Lab Subject Code: 21CSP-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 ArrayList.
• To learn about various methods of List.

3. Algorithm:
Create main class where you ask for your choice using switch case. Choice
can be:
1. Insert String in ArrayList/LinkedList:
2. Delete String from ArrayList/LinkedList:
3. Display Sring all elements of ArrayList/LinkedList:
4. Search String in ArrayList/LinkedList:
5. Exit

4. Code:
import java.util.ArrayList;
import java.util.Scanner;

public class StringListOperations { public


static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
boolean running = true;

while (running)

{ System.out.println("\

nSelect an option:");

System.out.println("1. Insert");
System.out.println("2. Delete");
System.out.println("3. Display");
System.out.println("4. Search");
System.out.println("5. Exit");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume the newline character

switch (choice) {
case 1:
System.out.println("Enter the string to
insert:"); String insertString =
scanner.nextLine(); insert(stringList,
insertString); break;
case 2:
System.out.println("Enter the string to
delete:"); String deleteString =
scanner.nextLine(); delete(stringList,
deleteString); break;
case 3:
display(stringList);
break;
case 4:
System.out.println("Enter the string to
search:"); String searchString =
scanner.nextLine(); search(stringList,
searchString); break;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
case 5:
running = false;
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");

}
}

scanner.close();
}

// Insert operation
private static void insert(ArrayList<String> list, String item)
{ list.add(item);
System.out.println(item + " inserted
successfully."); }

// Delete operation
private static void delete(ArrayList<String> list, String item) {
if (list.remove(item)) {
System.out.println(item + " deleted successfully.");
} else {
System.out.println(item + " not found in the
list."); }
}

// Display operation
private static void display(ArrayList<String> list)
{ if (list.isEmpty()) {
System.out.println("List is
empty."); } else {
System.out.println("List contents:");
for (String item : list) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.println(item);
}
}
}

// Search operation
private static void search(ArrayList<String> list, String item) {

if (list.contains(item)) {
System.out.println(item + " found in the list.");
} else {
System.out.println(item + " not found in the
list."); }
}
}
5. Output
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Learning outcomes:
• Learnt about concept of ArrayList.
• Learnt about various methods of List.

You might also like