0% found this document useful (0 votes)
37 views4 pages

Java 2.3 Sumit

Uploaded by

sumitsingla271
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)
37 views4 pages

Java 2.3 Sumit

Uploaded by

sumitsingla271
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

Experiment- 2.

Student Name: Sumit UID: 21BCS10075


Branch: BE-CSE Section/Group: KRG_SC_1
Semester: 6th Date of Performance: 07/03/2024
Subject Name: Project Based Learning in Java 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 ArrayList.
• To learn about various methods of List.

3. Script and Output:


import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nSelect operation:");
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();
switch (choice) {
case 1:
System.out.print("Enter string to insert: ");
String insertString = scanner.nextLine();
insert(stringList, insertString);
break;
case 2:
System.out.print("Enter string to delete: ");
String deleteString = scanner.nextLine();
delete(stringList, deleteString);
break;
case 3:
display(stringList);
break;
case 4:
System.out.print("Enter string to search: ");
String searchString = scanner.nextLine();
search(stringList, searchString);
break;
case 5:
System.out.println("Exiting program.");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
private static void insert(ArrayList<String> list, String item) {
list.add(item);
System.out.println("String '" + item + "' inserted successfully.");
}

private static void delete(ArrayList<String> list, String item) {


if (list.contains(item)) {
list.remove(item);
System.out.println("String '" + item + "' deleted successfully.");
} else {
System.out.println("String '" + item + "' not found in the list.");
}
}
private static void display(ArrayList<String> list) {
if (list.isEmpty()) {
System.out.println("List is empty.");
} else {
System.out.println("String list:");
for (String item : list) {
System.out.println(item);
}
}
}
private static void search(ArrayList<String> list, String item) {
if (list.contains(item)) {
System.out.println("String '" + item + "' found in the list.");
} else {
System.out.println("String '" + item + "' not found in the list.");
}
}
}
Output:

You might also like