6 java
6 java
Experiment 2.3
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
}
}
// 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: