-
Notifications
You must be signed in to change notification settings - Fork 4
/
ArrayList_Learn.java
322 lines (246 loc) · 9.58 KB
/
ArrayList_Learn.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/*
* An ArrayList is a dynamic-sized array. It grows and shrinks in size
* dynamically as we keep on adding or removing elements.
*/
public class ArrayList_Learn {
public static void main(String[] args) {
/*
* The 'List' interface extends the 'Collection' interface and declares the
* behavior of a collection that stores a sequence of elements. Elements can be
* added or accessed by their position in the list, using a zero-based index. A
* list may also contain duplicate elements.
*
* The 'ArrayList' class extends the 'AbstractList' class and implements the
* 'List' interface.
*/
List<Integer> numList = new ArrayList<>();
/*
* Adding elements to ArrayList
*
* boolean add(E obj) : Declared in the Collection interface. Adds object to the
* collection. Returns true if object was added, otherwise returns false.
*
* void add(int index, E obj) : Declared in the List interface. Adds object obj
* to the invoking list at the index passed. Any preexisting element at or
* beyond the index are shifted up. Thus no elements are overwritten.
*
* boolean addAll(Collection c) : Declared in the Collection interface. Adds all
* the elements of c to the invoking collection. Returns true if elements were
* added to the invoking collection. Otherwise, returns false.
*
* boolean addAll(int index, Collection c) : Declared in the List interface.
* Adds all the elements of c to the invoking list at the index passed. Any
* preexisting element at or beyond the index are shifted up. Thus no elements
* are overwritten. Returns true if elements were added to the invoking list.
* Otherwise, returns false.
*/
numList.add(7);
// numList = [7]
numList.add(5);
// numList = [7, 5]
numList.add(1, 3);
// numList = [7, 3, 5]
List<Integer> nums = new ArrayList<>(Arrays.asList(6, 3, 1));
numList.addAll(nums);
// numList = [7, 3, 5, 6, 3, 1]
nums = new ArrayList<>(Arrays.asList(4, 7));
numList.addAll(4, nums);
// numList = [7, 3, 5, 6, 4, 7, 3, 1]
System.out.println("numList = " + numList);
// numList = [7, 3, 5, 6, 4, 7, 3, 1]
/*
* Removing elements from ArrayList
*
* boolean remove(Object obj) : Declared in the Collection interface. Removes
* one instance of obj from the invoking collection. Returns true if the element
* was removed. Otherwise, returns false.
*
* boolean removeAll(Collection c) : Declared in the Collection interface.
* Removes all elements of c from the invoking collection. Returns true if
* elements were removed from the invoking collection. Otherwise, returns false.
*
* E remove(int index) : Declared in the List interface. Removes the element at
* the specified index from the invoking list, returning the element in the
* process. The indexes of the subsequent elements are decremented by one.
*
* boolean retainAll(Collection c) : Declared in the Collection interface.
* Removes all elements from the invoking collection except those in c. Returns
* true if elements were removed from the invoking collection. Otherwise,
* returns false.
*/
// numList = [7, 3, 5, 6, 4, 7, 3, 1]
numList.remove(Integer.valueOf(3));
// numList = [7, 5, 6, 4, 7, 3, 1]
nums = new ArrayList<>(Arrays.asList(6, 7));
// removes all occurrences of each element of nums from numList
numList.removeAll(nums);
// numList = [5, 4, 3, 1]
int removedElement = numList.remove(1);
System.out.println("removedElement = " + removedElement); // removedElement = 4
System.out.println("numList = " + numList);
// numList = [5, 3, 1]
numList.addAll(Arrays.asList(4, 7, 3, 1, 5));
// numList = [5, 3, 1, 4, 7, 3, 1, 5]
nums.addAll(Arrays.asList(5, 1));
// nums = [6, 7, 5, 1]
numList.retainAll(nums); // Elements [3, 4] are removed from numList
// numList = [5, 1, 7, 1, 5]
System.out.println("numList = " + numList);
// numList = [5, 1, 7, 1, 5]
/*
* Check if ArrayList contains an object
*
* boolean contains(Object obj) : Declared in the Collection interface. Returns
* true if obj is an element of the invoking collection. Otherwise, returns
* false.
*
* boolean containsAll(Collection c) : Declared in the Collection interface.
* Returns true if the invoking collection contains all elements of c.
* Otherwise, returns false.
*/
int value = 5;
// numList = [5, 1, 7, 1, 5]
if (numList.contains(value))
System.out.println("numList contains " + value);
else
System.out.println("numList does not contain " + value);
nums = new ArrayList<>(Arrays.asList(5, 7));
// numList = [5, 1, 7, 1, 5]
if (numList.containsAll(nums))
System.out.println("numList contains all elements of nums");
else
System.out.println("numList does not contain all elements of nums");
/*
* Get the element at an index in ArrayList
*
* E get(int index) : Declared in the List interface. Returns the object stored
* at the specified index within the invoking collection.
*/
// numList = [5, 1, 7, 1, 5]
int element = numList.get(2);
System.out.println("element = " + element); // element = 7
/*
* Get the index of an element in ArrayList
*
* int indexOf(Object obj) : Declared in the List interface. Returns the index
* of the first instance of obj in the invoking list. If obj is not present in
* the list, -1 is returned.
*
* int lastIndexOf(Object obj) : Declared in the List interface. Returns the
* index of the last instance of obj in the invoking list. If obj is not present
* in the list, -1 is returned.
*/
// numList = [5, 1, 7, 1, 5]
int lastIndex = numList.lastIndexOf(5);
System.out.println("lastIndex = " + lastIndex); // lastIndex = 4
/*
* Set the element at an index in ArrayList
*
* E set(int index, E obj) : Declared in the List interface. Assigns obj to the
* location specified by index within the invoking list. Returns the old value.
*/
// numList = [5, 1, 7, 1, 5]
int oldValue = numList.set(3, 9);
System.out.println("oldValue = " + oldValue); // oldValue = 1
System.out.println("numList = " + numList); // numList = [5, 1, 7, 9, 5]
/*
* Check if ArrayList is empty or not
*
* boolean isEmpty() : Declared in the Collection interface. Returns true if the
* invoking collection is empty. Otherwise, returns false.
*/
// numList = [5, 1, 7, 9, 5]
if (numList.isEmpty())
System.out.println("ArrayList is empty");
else
System.out.println("ArrayList is not empty");
/*
* Get the count of elements present in the ArrayList
*
* int size() : Declared in the Collection interface. Returns the number of
* elements held in the invoking collection.
*/
// numList = [5, 1, 7, 9, 5]
int listSize = numList.size();
System.out.println("Size = " + listSize); // Size = 5
/*
* Get sub-list from an ArrayList
*
* List<E> subList(int startIndex, int endIndex) : Declared in the List
* interface. Returns a list that includes elements from startIndex to (endIndex
* - 1) in the invoking list. Elements in the returned list are also referenced
* by the invoking object.
*/
// numList = [5, 1, 7, 9, 5]
List<Integer> subArrayList = new ArrayList<Integer>();
subArrayList = numList.subList(1, 4);
System.out.println("subArrayList = " + subArrayList); // subArrayList = [1, 7, 9]
/*
* Clear the ArrayList
*
* void clear() : Declared in the Collection interface. Removes all elements
* from the invoking collection.
*/
// numList = [5, 1, 7, 9, 5]
numList.clear();
System.out.println("numList = " + numList); // numList = []
/*
* Construct ArrayList from array
*/
String fruits[] = { "apple", "grape", "banana", "orange", "grape" };
List<String> fruitList = new ArrayList<>();
Collections.addAll(fruitList, fruits);
System.out.println("fruitList = " + fruitList); // fruitList = [apple, grape, banana, orange, grape]
/*
* Construct array from ArrayList
*/
String fruitArr[] = fruitList.toArray(new String[fruitList.size()]);
System.out.println("fruitArr = " + Arrays.toString(fruitArr)); // fruitArr = [apple, grape, banana, orange,
// grape]
/*
* Iterating over the contents of a ArrayList
*
* Iterator<E> iterator() : Declared in the Collection interface. Returns an
* iterator for the invoking collection.
*/
// fruitList = [apple, grape, banana, orange, grape]
Iterator itr = fruitList.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println();
// Iterate using for-each loop
// fruitList = [apple, grape, banana, orange, grape]
for (String fruit : fruitList) {
System.out.print(fruit + " ");
}
System.out.println();
/*
* ListIterator<E> listIterator() : Declared in the List interface. Returns an
* iterator to invoking list starting from the first element.
*/
// fruitList = [apple, grape, banana, orange, grape]
ListIterator listItr = fruitList.listIterator();
while (listItr.hasNext()) {
System.out.print(listItr.next() + " ");
}
System.out.println();
/*
* ListIterator<E> listIterator(int index) : Declared in the List interface.
* Returns an iterator to invoking list starting from the element at the
* specified index.
*/
// fruitList = [apple, grape, banana, orange, grape]
ListIterator listIndexItr = fruitList.listIterator(2);
while (listIndexItr.hasNext()) {
System.out.print(listIndexItr.next() + " ");
}
System.out.println();
}
}