-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListExample.java
More file actions
51 lines (40 loc) · 1.16 KB
/
ArrayListExample.java
File metadata and controls
51 lines (40 loc) · 1.16 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
package collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
List<String> countryList = new LinkedList<String>();
countryList.add("Nepal");
countryList.add("China");
countryList.add("USA");
countryList.add("Japan");
if (countryList.contains("Japan")) {
System.out.println("Yes there is Japan.");
}
// Iterator can be used to traverse.
Iterator<String> itr = countryList.iterator();
while (itr.hasNext()) {
String s = itr.next();
System.out.println(s);
// we can remove elements inside iterator.
if (s.equals("China")) {
itr.remove();
}
}
countryList.remove(2);
String npl = countryList.get(0);
System.out.println("No of elments left: " + countryList.size());
// Foreach: display purpose
for (String s : countryList) {
System.out.println(s);
}
for (int i = 0; i < countryList.size(); i++) {
String s = countryList.get(i);
countryList.remove(i);
System.out.println(s);
}
countryList.clear();
System.out.println(countryList.isEmpty());
}
}