Skip to content

Commit 431dfba

Browse files
authored
Merge pull request codec-akash#13 from AnkitDua123/master
Added Collection ArrayList program
2 parents ee637fe + ca2e5de commit 431dfba

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

ArrayLists.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import java.util.ListIterator;
8+
9+
public class ArrayLists {
10+
public static final Integer[] i = new Integer[] { 10, 20, 30 }; // [0,20,30]
11+
public static final Integer[] j = new Integer[] { 60, 70 }; // [60,70]
12+
public static final Integer[] k = new Integer[] { 80, 90 }; // [80,90]
13+
14+
public static void main(String[] args) {
15+
16+
// Create/Initialize List (default)
17+
List<Integer> ls_default = new ArrayList<Integer>(); // []
18+
// Create/Initialize List with initial capacity 10
19+
List<Integer> ls_initial_size = new ArrayList<Integer>(10); // [ , , , , , , , , , ]
20+
// Create/Initialize List with existing Collection
21+
List<Integer> ls_collection = new ArrayList<>(Arrays.asList(i)); // [0,20,30]
22+
23+
//-----Add Values-------------//
24+
ls_collection.add(50); // [0,20,30,50]
25+
ls_collection.add(3, 40); // [0,20,30,40,50]
26+
ls_collection.addAll(Arrays.asList(k)); // [0,20,30,40,50,80,90]
27+
ls_collection.addAll(5, Arrays.asList(j)); // [0,20,30,40,50,60,70,80,90]
28+
29+
//-----Size--------//
30+
ls_collection.size(); // 9
31+
ls_collection.isEmpty(); // false
32+
33+
//-----Find elements----//
34+
ls_collection.indexOf(30); // 2
35+
ls_collection.lastIndexOf(50); // 4
36+
ls_collection.contains(90); // true
37+
ls_collection.containsAll(Arrays.asList(i)); // true
38+
39+
//-----Get and set elements-----//
40+
ls_collection.get(4); // 30
41+
ls_collection.set(0, 10); // [10,20,30,40,50,60,70,80,90]
42+
ls_collection.subList(0, 3); // [10, 20, 30]
43+
44+
//-----Loop over each List item----//
45+
System.out.println(ls_collection.toString()); // [10, 20, 30, 40, 50, 60, 70, 80, 90]
46+
47+
ls_collection.forEach((val) -> System.out.print(val + " ")); // 10 20 30 40 50 60 70 80 90
48+
System.out.println();
49+
50+
for (Integer val : ls_collection) {
51+
System.out.print(val + " ");
52+
} // 10 20 30 40 50 60 70 80 90
53+
System.out.println();
54+
55+
ListIterator<Integer> it = ls_collection.listIterator();
56+
while (it.hasNext()) {
57+
System.out.print(it.next() + " ");
58+
} // 10 20 30 40 50 60 70 80 90
59+
System.out.println();
60+
61+
//-----Remove elements----//
62+
ls_collection.remove(0); // [20,30,40,50,60,70,80,90]
63+
ls_collection.remove(ls_collection.size() - 1); // [20,30,40,50,60,70,80]
64+
ls_collection.remove(ls_collection.indexOf(20)); // [30,40,50,60,70,80]
65+
ls_collection.removeAll(Arrays.asList(j)); // [30,40,50,80]
66+
ls_collection.clear(); // []
67+
68+
//----Sort---//
69+
ls_default.add(60);
70+
ls_default.add(20);
71+
ls_default.add(80);
72+
ls_default.add(10);
73+
Collections.sort(ls_default); // [10, 20, 60, 80]
74+
Collections.sort(ls_default, Collections.reverseOrder()); // [80, 60, 20, 10]
75+
Collections.sort(ls_default, new Comparator<Integer>() {
76+
@Override
77+
public int compare(Integer a, Integer b) {
78+
return a.compareTo(b);
79+
}
80+
}); // [10, 20, 60, 80]
81+
}
82+
}

0 commit comments

Comments
 (0)