Experiment 2.
Student Name: Ankit Kumar Malik UID: 21BCS9631
Branch: CSE Section/Group: CC_648_B
Semester: 6th Date of Performance:14/02/24
Subject Name: Java with Lab 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: 1. To learn about concept of ArrayList.
2. To learn about various methods of List.
3. Code:
import java.util.ArrayList;
import java.util.Scanner;
public class Exp2_3 {
public static void main(String args[]){
Scanner sc = new Scanner(System.in); int ch;
String s;
ArrayList<String> l = new ArrayList<String>();
do {
System.out.println("1.Insert \n2.Delete \n3.Display \n4.Search \n5.Exit \n");
System.out.println("Enter your choice:");
ch = Integer.parseInt(sc.nextLine());
switch (ch) {
case 1:
System.out.println("Enter the string to be inserted");
s = sc.nextLine(); l.add(s);
System.out.println("String inserted Successfully");
break; case 2:
System.out.println("Enter the string to be deleted");
s = sc.nextLine(); if (l.contains(s)) {
l.remove(s);
System.out.println("String deleted Successfully");
} else {
System.out.println("String not found");
}
break;
case 3:
System.out.println("The list is: ");
for (String i : l)
System.out.println(i);
break;
case 4:
System.out.println("Enter the string to be searched");
s = sc.nextLine();
if (l.contains(s))
System.out.println("Item present in the list");
else
System.out.println("Item is not present in the list");
break; case 5:
System.out.println("Exiting...");
System.exit(0);
}
} while (ch != 5);
}
}
6. Output:
7. Learning Outcomes:
• Gain knowledge about the ArrayList data structure in Java, which allows dynamic
resizing of arrays.
• Learn how to declare, initialize, and manipulate ArrayList objects to store and
manage collections of String elements.
• Comprehend the advantages of using ArrayList over traditional arrays, such as
flexibility in size and built-in methods for easy manipulation..