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

6 java

Uploaded by

www.annie.201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

6 java

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.3

Student Name: Ansh UID: 21BCS5255


Branch: B.E CSE Section/Group: 639-A
Semester: 6th Date of Performance:
Subject Name: JAVA Subject Code: 21CSP-314

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. Program Code:
package java_programs;
import java.util.ArrayList;
import java.util.Scanner;
public class experiment_6 {
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
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:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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;
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.");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
}
// 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) {
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.");
}
}
}
4. Output:

Fig. 1: Insert
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Fig. 2: Delete

Fig. 3: Display

Fig. 4: Search

5. Learning Outcomes:

 Learned about concept of ArrayList.


 Learned about various methods of List.
 Learned about various operations in list.

You might also like