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