-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01a_array_list.java
62 lines (50 loc) · 1.58 KB
/
01a_array_list.java
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
import java.util.*;
class MyCollections {
public static void main(String args[]) {
example_arrayList_string();
example_arrayList_int();
}
public static void example_arrayList_string() {
// Create using
// ArrayList <Data Type> variable = new ArrayList <Data Type>();
ArrayList<String> workingDays = new ArrayList<String>();
workingDays.add("Monday");
workingDays.add("Tuesday");
workingDays.add("Wednesday");
workingDays.add("Friday");
// Add to a specific index
workingDays.add(3, "Thursday");
// Iterate over the array list
Iterator itor = workingDays.iterator();
System.out.println("\nWorkdays");
while( itor.hasNext() ) {
System.out.println( itor.next() );
}
// function to check whether the value is in ArrayList
if( workingDays.contains("Saturday") == false) {
System.out.println("Workdays does not conatin Saturday");
} else {
System.out.println("Workdays does conatin Saturday");
}
System.out.println("Clearing the array list...");
workingDays.clear();
// Iterate over the array list
itor = workingDays.iterator();
System.out.println("\nWorkdays");
while( itor.hasNext() ) {
System.out.println( itor.next() );
}
}
public static void example_arrayList_int() {
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(10);
scores.add(20);
scores.add(20);
scores.add(10);
Iterator itor = scores.iterator();
System.out.println("\nScores");
while( itor.hasNext() ) {
System.out.println( itor.next() );
}
}
}