-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListTest.java
More file actions
72 lines (52 loc) · 1.55 KB
/
Copy pathArrayListTest.java
File metadata and controls
72 lines (52 loc) · 1.55 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package collections;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
System.out.println(al);
System.out.println(al.size());
System.out.println("********************");
System.out.println("Using For Loop");
for(int i=0; i<al.size();i++) {
System.out.println(al.get(i));
}
System.out.println("********************");
System.out.println("Through Iterator");
Iterator<Integer> itr=al.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("********************");
System.out.println("Using While");
int num=0;
while(num<al.size()) {
System.out.println(al.get(num));
num++;
}
System.out.println("********************");
System.out.println("Using Advanced For Loop");
for(Integer i:al) {
System.out.println(i);
}
ArrayList<Integer> al2 = new ArrayList<Integer>();
al2.addAll(al);
System.out.println("al2 contents " +al2);
ArrayList<Integer> al3 = new ArrayList<Integer>();
al3.addAll(al2);
al3.add(100);
al3.removeAll(al2);
System.out.println("al2 contents " +al3);
ArrayList<Integer> al4 = new ArrayList<Integer>();
al4.addAll(al2);
al4.add(60);
al4.add(70);
System.out.println("al4 contents " +al4);
al4.retainAll(al2);
System.out.println("al4 contents now " +al4);
}
}