Skip to content

Commit 70c8c45

Browse files
committed
Collection FrameWork folder added
1 parent a5d2633 commit 70c8c45

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
import java.util.ArrayList;
3+
4+
public class DemoArrayList {
5+
public static void main(String[] args) {
6+
// Create an ArrayList of integers
7+
ArrayList al = new ArrayList();
8+
9+
//Arraylist <String> al = new ArrayList<String>(); // ArrayList<String> this is generic array
10+
// used to defiine type parameters
11+
12+
//checking whether list is empty or not
13+
boolean isEmpty = al.isEmpty();
14+
System.out.println("Is the ArrayList empty? " + isEmpty);
15+
16+
// Add elements to the ArrayList
17+
al.add(10);
18+
al.add(20);
19+
al.add(20);
20+
al.add(20);
21+
al.add("hello");
22+
al.add(30);
23+
al.add("hello");
24+
al.add("hello");
25+
al.add(50.20);
26+
al.add("hello world");
27+
28+
29+
30+
// Print the ArrayList
31+
System.out.println("ArrayList: " + al);
32+
33+
// Get the size of the ArrayList
34+
int size = al.size();
35+
System.out.println("Size of the ArrayList: " + size);
36+
37+
38+
// Get the element at a specific index
39+
Object element = al.get(2);
40+
System.out.println("Element at index 2: " + element);
41+
42+
// Remove an element from the ArrayList
43+
al.remove(2);
44+
System.out.println("Updated ArrayList: " + al);
45+
46+
47+
System.out.println("Before update" + al);
48+
//Set an element at a specific index
49+
al.set(1, "Adding at 01 index");
50+
System.out.println("Updated ArrayList: " + al);
51+
52+
53+
// Check if an element exists in the ArrayList
54+
boolean exists = al.contains(30);
55+
System.out.println("Element 30 exists in the ArrayList: " + exists);
56+
//or
57+
// System.out.println(al.contains("hello"));
58+
59+
60+
61+
62+
63+
// Clear the ArrayList
64+
// al.clear();
65+
// System.out.println("After clearing the ArrayList: " + al);
66+
67+
// retain all the elements
68+
al.retainAll(al);
69+
System.out.println("After retain " + al);
70+
71+
//trnasversing arrays
72+
System.out.println("Before traversal: " + al);
73+
for (Object obj : al) { //traversing using for each element in the array
74+
System.out.println(obj);
75+
}
76+
System.out.println("After traversal: " + al);
77+
78+
for(int i = 0; i<al.size(); i++){ //using for loop
79+
System.out.println(al.get(i));
80+
}
81+
82+
83+
84+
85+
}
86+
87+
88+
// Add more methods here to perform other operations on the ArrayList
89+
// For example, finding the maximum or minimum element, sorting the ArrayList, etc.
90+
// Feel free to add as many methods as you want!
91+
}
92+
// Note: The ArrayList provided here is a generic ArrayList, meaning it can store elements of any type.
93+
// If you want to store elements of a specific type, you can use a different type of ArrayList, such as ArrayList<String> for storing strings.
94+
// You can also use custom objects as elements by implementing the Comparable interface in your custom class.
95+
// For example, if you have a class called "Person" with a name and age, you can create an ArrayList<Person> to store Person objects.
96+
// If you need more advanced features or need to store custom objects, you may want to consider using a different data structure or library, such as Java's Collections Framework or Apache Commons Collections.
97+
// Also, remember to handle any potential exceptions that may occur when using the ArrayList, such as IndexOutOfBoundsException or NullPointerException.
98+
99+
12.1 KB
Loading

0 commit comments

Comments
 (0)