Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Exp 2.3 Java WS

Rohan Kumar completed an experiment on performing basic operations like insert, delete, display, and search on a list of string objects using an ArrayList. The program takes user input for the desired operation and performs it, including adding elements to the list, removing elements, searching for elements, and displaying the full list. Key steps included creating an ArrayList to store the elements, a switch statement to handle the different operations, and using for loops and if statements to implement the specific functionality of each operation on the list. The student learned about using collections like ArrayList and the various operations that can be performed on ArrayLists.

Uploaded by

madhu jha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Exp 2.3 Java WS

Rohan Kumar completed an experiment on performing basic operations like insert, delete, display, and search on a list of string objects using an ArrayList. The program takes user input for the desired operation and performs it, including adding elements to the list, removing elements, searching for elements, and displaying the full list. Key steps included creating an ArrayList to store the elements, a switch statement to handle the different operations, and using for loops and if statements to implement the specific functionality of each operation on the list. The student learned about using collections like ArrayList and the various operations that can be performed on ArrayLists.

Uploaded by

madhu jha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.3

Student Name: Rohan Kumar UID: 20BCS2864


Branch: CSE Section/Group: 20BCS_MM-807_A
Semester: 5 Date of Performance: 14/9/2022
Subject Name: PBLJ Lab Subject Code: 20CSP-321

1. Aim/Overview of the practical:


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. Software/Hardware Requirements:

o Laptop
o Eclipse IDE

3. Algorithm /pseudo code:

Step 1- Create an ArrayList.

Step 2- Then take input from user for which operation to perform, if
user enter a invalid number then print suitable message.

Step 3- If user enter 1, then add number entered by user in the list and
go to step 2.

Step 4- If user enter 2, then search for that element and if found then
remove it and go to step 2.

Step 5- If user enter 3 then search for that element and print it if found,
else print suitable message.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 6- If user enter 4, then print entire list and go to step 2.

Step 7- If user enter 5 , then exit the program.

4. Steps for experiment/practical/Code:


import java.util.*;

public class Main {

public static void main(String[] args) {


List<String> list = new ArrayList<String>();
int status = 1;
Scanner sc = new Scanner(System.in);
while (status == 1) {
System.out.println("Kindly choose an option");
System.out.println("1. Insert in list");
System.out.println("2. Delete from list");
System.out.println("3. Search in list");
System.out.println("4. Display List");
System.out.println("5. Exit");
int c = sc.nextInt();
String s = " ";
switch (c) {
case 1:
System.out.println("Enter the element that you want to
insert");
s = sc.next();
list.add(s);
System.out.println("Element inserted successfully");
break;
case 2:
System.out.println("Enter the element that you want to
delete");
s = sc.next();
for (int i = 0; i < list.size(); i++) {

if (list.get(i).equalsIgnoreCase(s)) {
System.out.println(list.get(i) + " removed from
list");
list.remove(list.get(i));

}
}
break;

case 3:
System.out.println("Enter the element that you want to
search");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

s = sc.next();
int flag = 0;
for (int i = 0; i < list.size(); i++) {

if (list.get(i).equalsIgnoreCase(s)) {
System.out.println(list.get(i));
System.out.println(list.get(i) + " found at index " +
i);
flag = 1;
}
}
if (flag != 1) {
System.out.println("Item not found");
}
break;

case 4:
for (int i = 0; i < list.size(); i++) {

System.out.println(list.get(i));

}
break;

case 5:
status = 0;
break;

default:
System.out.println("Invalid input");

}
}
}

5. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning outcomes (What I have learnt):

1. Learnt about use of collections, ArrayList.

2. Learnt about various operation of ArrayList.

You might also like