Experiment-9---JAVA
AIM
To create java program to add, retrieve and remove the element from the ArrayList.
ALGORITHM
- Declare a main method using public class Main.
- Create an ArrayList named "list"
- Add elements to ArrayList using the command "Arraylistname.add()".
- Retrive an element from the ArrayList using "get" command and print the same.
- Remove the element from the ArrayList using "remove" command and print the same.
- Print the updated list and end the program.
PROGRAM
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(); // Create an ArrayList
// Adding elements to the ArrayList
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Durian");
System.out.println("ArrayList: " + list);
// Retrieve element at index 2
String retrievedElement = list.get(2);
System.out.println("Retrieved element at index 2: " + retrievedElement);
// Remove element at index 1
String removedElement = list.remove(1);
System.out.println("Removed element at index 1: " + removedElement);
System.out.println("Updated ArrayList: " + list);
}
}
OUTPUT
![image](https://private-user-images.githubusercontent.com/93427376/241616763-baab5b85-b4a8-4ed4-a859-49de7fd856ed.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MDUyMTMxODgsIm5iZiI6MTcwNTIxMjg4OCwicGF0aCI6Ii85MzQyNzM3Ni8yNDE2MTY3NjMtYmFhYjViODUtYjRhOC00ZWQ0LWE4NTktNDlkZTdmZDg1NmVkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDAxMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwMTE0VDA2MTQ0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE1MzhhNzVlYTcwMjNlYzQ1MGFlYWI0MDYxNmRiYWE2MTQyM2MzMDAxM2ExMzQ0ZTdiMWYxZjllMWI4NzEzYjQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.38C3t9KjXLUQIdmvZSDakgKbU1dGARPLQy-Kvz1UKzs)
RESULT
Thus a program is created to add,retrive and remove an element from an ArrayList.