-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionsArrayList.java
More file actions
40 lines (33 loc) · 952 Bytes
/
Copy pathCollectionsArrayList.java
File metadata and controls
40 lines (33 loc) · 952 Bytes
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
package collectionsInJava;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CollectionsArrayList {
public static void main(String[] args) {
List arraylistobj = new ArrayList();
arraylistobj.add(10);
arraylistobj.add(20);
arraylistobj.add("test");
arraylistobj.add('N');
System.out.println(arraylistobj);
//traversal using for loop.
for(int i=0;i<arraylistobj.size();i++) {
System.out.print(arraylistobj.get(i)+" ");
}
System.out.println();
//traversal using for each loop.
for(Object element : arraylistobj) {
System.out.print(element+" ");
}
System.out.println();
//traversal using while loop
Iterator itr = arraylistobj.iterator();
while(itr.hasNext()) {
System.out.print(itr.next()+" ");
}
System.out.println();
//remove element at index.
arraylistobj.remove(2);
System.out.println(arraylistobj);
}
}