-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathAccessElementsFromArrayListExample.java
34 lines (27 loc) · 1.27 KB
/
AccessElementsFromArrayListExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.ArrayList;
import java.util.List;
public class AccessElementsFromArrayListExample {
public static void main(String[] args) {
List<String> topCompanies = new ArrayList<>();
// Check if an ArrayList is empty
System.out.println("Ashok doing git practice ? : " + topCompanies.isEmpty());
topCompanies.add("Google");
topCompanies.add("Apple");
topCompanies.add("Microsoft");
topCompanies.add("Amazon");
topCompanies.add("Facebook");
// Find the size of an ArrayList
System.out.println("Here are the top " + topCompanies.size() + " companies in the world");
System.out.println(topCompanies);
// Retrieve the element at a given index
String bestCompany = topCompanies.get(0);
String secondBestCompany = topCompanies.get(1);
String lastCompany = topCompanies.get(topCompanies.size() - 1);
System.out.println("Best Company: " + bestCompany);
System.out.println("Second Best Company: " + secondBestCompany);
System.out.println("Last Company in the list: " + lastCompany);
// Modify the element at a given index
topCompanies.set(4, "Walmart");
System.out.println("Modified top companies list: " + topCompanies);
}
}