DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 2.3
Student Name: Preeti Jaiswal UID: 21BCS3439
Branch: BE-CSE Section/Group:CC-644-B
Semester: 6 Subject Code:21CSH-319
Subject Name: Java Lab Date of Performance:06-03-2024
1. Aim: Program to perform the basic operations on String List
1. Objective: 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. Algo. /Approach and output:
import [Link];
import [Link];
public class StringListOperations {
private static ArrayList<String> stringList = new ArrayList<>();
private static Scanner scanner = new Scanner([Link]);
public static void main(String[] args) {
boolean exit = false;
while (!exit) {
[Link]("\n1. Insert\n2. Delete\n3. Display\n4. Search\n5. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
[Link](); // consume newline character
switch (choice) {
case 1:
insert();
break;
case 2:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
delete();
break;
case 3:
display();
break;
case 4:
search();
break;
case 5:
exit = true;
break;
default:
[Link]("Invalid choice!");
}
}
[Link]();
}
private static void insert() {
[Link]("Enter string to insert: ");
String item = [Link]();
[Link](item);
[Link]("String inserted successfully!");
}
private static void delete() {
if ([Link]()) {
[Link]("List is empty!");
return;
}
[Link]("Enter index to delete: ");
int index = [Link]();
[Link](); // consume newline character
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (index < 0 || index >= [Link]()) {
[Link]("Invalid index!");
return;
}
String removedItem = [Link](index);
[Link]("String \"" + removedItem + "\" deleted successfully!");
}
private static void display() {
if ([Link]()) {
[Link]("List is empty!");
return;
}
[Link]("String List:");
for (int i = 0; i < [Link](); i++) {
[Link](i + ": " + [Link](i));
}
}
private static void search() {
if ([Link]()) {
[Link]("List is empty!");
return;
}
[Link]("Enter string to search: ");
String searchItem = [Link]();
int index = [Link](searchItem);
if (index != -1) {
[Link]("String found at index " + index);
} else {
[Link]("String not found in the list!");
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Insert a string: - Delete and Display a String: -
Search a String: -
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes: -
• Understanding of ArrayList: Understood the usage of ArrayList to store and manipulate a
dynamic collection of String objects.
• User Interaction: Implemented user interaction through console input, enhancing
understanding of basic I/O operations.
• Switch-Case Control Flow: Familiarization with switch-case statements for menu-driven
program flow control.
• Basic String Operations: Practiced with basic string manipulation, including insertion,
deletion, and search functionalities.
• Error Handling: Learned to handle common errors such as out-of-bounds index access
and empty list scenarios, fostering robust programming habits.