You can modify an ArrayList in Java with add methods to add elements into the list, set method to replace element and remove methods to remove element from the list

For all index operations, IndexOutOfBoundsException will be thrown if the index is out of range

Add elements into an ArrayList

  • add(Object) method add (appends) the specified element into the end of the list
List<Integer> lst = new ArrayList<>();  
lst.add(3);  
lst.add(1);  
lst.add(3);  
lst.add(null);

System.out.println(lst); // [3, 1, 3, null]

ArrayList allows duplicate and null elements and preserve the insertion order of theirs elements so the output of the above example [3, 1, 3, null] is always consistent

  • add(int, Object) method inserts the specified element at the specified index in the list and shifts the current and any subsequent elements to the right
List<Integer> lst = new ArrayList<>();  
lst.add(1);  
lst.add(2);  
lst.add(0, 3); // insert 3 at index 0, the list now is {3, 1, 2}  
lst.add(3, 10); // throws `IndexOutOfBoundsException`
  • addAll(Collection) method appends all elements of the specified collection into the end of the caller list, in the order returned by the specified collection's iterator
List<Integer> lst1 = new ArrayList<>();  
lst1.add(3);  
lst1.addAll(Arrays.asList(1, 2));  
System.out.println(lst1); // [3, 1, 2]

List<Integer> lst2 = new ArrayList<>();  
lst2.add(3);  
lst2.addAll(Set.of(1, 2));  
System.out.println(lst2); // [3, 1, 2]

The output of the lst2 may be different on your side as Set has no guarantees on the element orders

  • addAll(int, Collection) method inserts all elements of the specified collection into the list at the specified index and shifts the current and any subsequence elements to the right
List<Integer> lst = new ArrayList<>();  
lst.add(1);  
lst.add(2);  
lst.addAll(0, Arrays.asList(3));  
System.out.println(lst); // [3, 1, 2]


Update element in an ArrayList

  • set(int, Object) method replaces the element at the specified index with the specified element
List<Integer> lst = new ArrayList<>();  
lst.add(1);  
lst.add(1);  
lst.add(2);  
lst.set(0, 3);

System.out.println(lst); // [3, 1, 2]  


Remove elements from an ArrayList

  • remove(Object) method removes the first occurrence of the specified element from the list if it is present
List<Integer> lst = new ArrayList<>();  
lst.add(3);  
lst.add(1);  
lst.add(3);

lst.remove(Integer.valueOf(3));  
System.out.println(lst); // [1, 3]  
  • remove(int) method removes the element at the specified index and shifts any subsequence elements to the left
List<Integer> lst = new ArrayList<>();  
lst.add(3);  
lst.add(1);  
lst.add(3);

lst.remove(0);  
System.out.println(lst); // [1, 3]

lst.remove(3); // throws IndexOutOfBoundsException  
  • removeAll(Collection) method removes from the list all elements which contained in the specified collection
List<Integer> lst = new ArrayList<>();  
lst.add(3);  
lst.add(1);  
lst.add(3);

lst.removeAll(List.of(4, 1, 3));  
System.out.println(lst); // [] - the list is empty now  
  • retainAll(Collection) method retains elements in the list which contained in the specified collection
List<Integer> lst = new ArrayList<>();  
lst.add(3);  
lst.add(1);  
lst.add(3);

lst.removeAll(List.of(2, 3));  
System.out.println(lst); // [3, 3]  
  • void clear() method removes all elements from the list
List<Integer> lst = new ArrayList<>();  
lst.add(3);  
lst.add(1);  
lst.add(3);

lst.clear();  
System.out.println(lst); // [] - the list now is empty