public class TestArrayListOperations
1: //TestArrayListOperations.java
2: //Illustrates most of what are (probably) the most useful and
3: //frequently used ArrayList methods. As you experiment with this
4: //program, you should check the official ArrayList documentation
5: //and compare what you see there with what you see here.
6: //See TestArrayList_toArray.java for illustrations of toArray().
8: import java.util.ArrayList;
9: import java.util.Arrays;
11: public class TestArrayListOperations
12: {
13: public static void main(String[] args)
14: {
15: //Illustrates the default constructor and methods add(value),
16: //and size(), as well as indexOf(value) and lastIndexOf(value).
17: //Creates a new (empty) ArrayList of Integer and displays its size.
18: //Then adds some values and displays the size again.
19: //Note that autoboxing happens as the values are added.
20: //Then displays the first and last index locations of the value 2.
21: //Finally, displays all of the values.
22: System.out.println("=====1==============================");
23: ArrayList<Integer> arrayList1 = new ArrayList<>();
24: System.out.println("Size = " + arrayList1.size());
25: arrayList1.add(5);
26: arrayList1.add(2);
27: arrayList1.add(9);
28: arrayList1.add(4);
29: arrayList1.add(7);
30: arrayList1.add(2);
31: System.out.println("Size = " + arrayList1.size());
32: System.out.println("First index of 2 = " + arrayList1.indexOf(2));
33: System.out.println("Last index of 2 = " + arrayList1.lastIndexOf(2));
34: for (int i : arrayList1)
35: {
36: System.out.print(i + " ");
37: }
38: System.out.println();
40: //Illustrates methods add(index, value), contains(value)
41: //remove(index) and remove(value), removeIf(condition),
42: //as well as how to remove a range of values, which involves
43: //the use of subList(start, end).
44: System.out.println("=====2==============================");
45: System.out.println("Is 6 present? " + arrayList1.contains(6));
46: arrayList1.add(3, 6);
47: for (int i : arrayList1)
48: {
49: System.out.print(i + " ");
50: }
51: System.out.println();
52: System.out.println("Is 6 present? " + arrayList1.contains(6));
53: arrayList1.remove(5); //Note carefully. Compare with remove() below.
54: for (int i : arrayList1)
55: {
56: System.out.print(i + " ");
57: }
58: System.out.println();
59: Integer iObj = 5;
60: arrayList1.remove(iObj); //Note carefully. Compare with remove() above.
61: for (int i : arrayList1)
62: {
63: System.out.print(i + " ");
64: }
65: System.out.println();
66: arrayList1.removeIf(i -> i % 2 == 1);
67: for (int i : arrayList1)
68: {
69: System.out.print(i + " ");
70: }
71: System.out.println();
72: //arrayList1.removeRange(1, 3);
73: //Somewhat surprisingly, the above line will not compile.
74: //The reason is simple: it's a protected method. The reason
75: //it's protected is a little more complicated and we do not
76: //discuss it here. Google will fill you in ...
77: //Here's the alternative:
78: arrayList1.subList(1, 3).clear();
79: for (int i : arrayList1)
80: {
81: System.out.print(i + " ");
82: }
83: System.out.println();
85: //Illustrates ArrayList constructor that takes a container as input.
86: //Uses the same values as for the first container.
87: System.out.println("=====3==============================");
88: ArrayList<Integer> arrayList2 =
89: new ArrayList<>(Arrays.asList(5, 2, 9, 4, 7, 2));
90: for (int i : arrayList2)
91: {
92: System.out.print(i + " ");
93: }
94: System.out.println();
96: //Illustrate clear() and isEmpty().
97: System.out.println("=====4==============================");
98: arrayList2.clear();
99: for (int i : arrayList2)
100: {
101: System.out.print(i + " ");
102: }
103: System.out.println("\nAbove line contains values after clear().");
104: if (arrayList2.isEmpty())
105: {
106: System.out.println("The ArrayList is now empty.");
107: }
109: //Illustrate sort(), the default version as well as two
110: //different lambda functions for determining how to sort.
111: ArrayList<Integer> arrayList3 =
112: new ArrayList<>(Arrays.asList(5, 2, 9, 4, 7, 2));
113: arrayList3.sort(null); //Sorts using the "natural order"
114: for (int i : arrayList3)
115: {
116: System.out.print(i + " ");
117: }
118: System.out.println();
119: //The following two lines both sort in reverse,
120: //but the second is the preferred option.
121: arrayList3.sort((i1, i2) -> i2 - i1);
122: arrayList3.sort((i1, i2) -> Integer.compare(i2, i1));
123: for (int i : arrayList3)
124: {
125: System.out.print(i + " ");
126: }
127: System.out.println();
128: }
129: }
130: /* Output:
131: =====1==============================
132: Size = 0
133: Size = 6
134: First index of 2 = 1
135: Last index of 2 = 5
136: 5 2 9 4 7 2
137: =====2==============================
138: Is 6 present? false
139: 5 2 9 6 4 7 2
140: Is 6 present? true
141: 5 2 9 6 4 2
142: 2 9 6 4 2
143: 2 6 4 2
144: 2 2
145: =====3==============================
146: 5 2 9 4 7 2
147: =====4==============================
149: Above line contains values after clear().
150: The ArrayList is now empty.
151: 2 2 4 5 7 9
152: 9 7 5 4 2 2
153: */
155: /*
156: Just a note in passing ... below is another way to create
157: an ArrayList (of strings), which would require these imports:
158: import java.util.Collections;
159: import java.util.stream.Stream;
161: Stream.of("xyz", "abc").collect(Collectors.toList());
162: You can ignore this approach for the time being.
163: */