You can modify an ArrayList in Java with add
methods to append and insert elements into the list, set
method to replace element, remove
methods to remove element from the list
For all index operations, IndexOutOfBoundsException
will be thrown if the index is out of range
Lets walk through this tutorial to explore them in more details
Append elements into an ArrayList
add(Object)
method add (appends) the specified element into the end of the list
@Test
public void append() {
List<Integer> lst = new ArrayList<>(Arrays.asList(3, 1, 2));
// Appends elements to lst
lst.add(4);
// Can append null element
lst.add(null);
// Can append duplicate element
lst.add(2);
// Asserts that the list contains exactly the given values in order
assertThat(lst).containsExactly(3, 1, 2, 4, null, 2);
}
ArrayList allows duplicate and null elements and preserve the insertion order of theirs elements
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
@Test
public void appendAll() {
List<Integer> lst = new ArrayList<>();
lst.add(3);
// Appends all elements to lst
lst.addAll(Arrays.asList(1, 2));
assertThat(lst).containsExactly(3, 1, 2);
}
Insert element at the specified location
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
@Test
public void insert() {
List<Integer> lst = new ArrayList<>();
lst.add(1);
lst.add(2);
// Inserts element to lst at the specified index
lst.add(0, 3);
assertThat(lst).containsExactly(3, 1, 2);
// Asserts that the list throws IndexOutOfBoundsException
// when insert element at the out of bound position
assertThatThrownBy(() -> lst.add(5, 4))
.isInstanceOf(IndexOutOfBoundsException.class);
}
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
@Test
public void insertAll() {
List<Integer> lst = new ArrayList<>();
lst.add(2);
// Inserts a collection of elements to lst at the specified index
lst.addAll(0, Arrays.asList(3, 1));
assertThat(lst).containsExactly(3, 1, 2);
assertThatThrownBy(() -> lst.addAll(4, Arrays.asList(4, 5)))
.isInstanceOf(IndexOutOfBoundsException.class);
}
Update element in an ArrayList
set(int, Object)
method replaces the element at the specified index with the specified element
@Test
public void set() {
List<Integer> lst = new ArrayList<>();
lst.add(1);
lst.add(1);
lst.add(2);
// Updates element at the specified index
lst.set(0, 3);
assertThat(lst).containsExactly(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
@Test
public void removeElement() {
List<Integer> lst = new ArrayList<>();
lst.add(3);
lst.add(1);
lst.add(3);
// Removes the first occurrence of the specified element from lst
lst.remove(Integer.valueOf(3));
assertThat(lst).containsExactly(1, 3);
}
remove(int)
method removes the element at the specified index and shifts any subsequence elements to the left
@Test
public void removeAtIndex() {
List<Integer> lst = new ArrayList<>();
lst.add(3);
lst.add(1);
lst.add(3);
// Removes element at the specified position in lst
lst.remove(0);
assertThat(lst).containsExactly(1, 3);
assertThatThrownBy(() -> lst.remove(2))
.isInstanceOf(IndexOutOfBoundsException.class);
}
removeAll(Collection)
method removes from the list all elements which contained in the specified collection
@Test
public void removeAll() {
List<Integer> lst = new ArrayList<>();
lst.add(3);
lst.add(1);
lst.add(3);
// Removes elements in lst that are contained in the specified collection
lst.removeAll(List.of(4, 1, 3));
assertThat(lst).isEmpty();
}
Retains elements in an ArrayList
retainAll(Collection)
method retains elements in the list which contained in the specified collection
@Test
public void retainAll() {
List<Integer> lst = new ArrayList<>();
lst.add(3);
lst.add(1);
lst.add(3);
// Retains elements in lst that are contained in the specified collection
lst.retainAll(List.of(2, 3));
assertThat(lst).containsExactly(3, 3);
}
Clear an ArrayList
clear()
method removes all of the elements from the list, the list will be empty after this call
@Test
public void clear() {
List<Integer> lst = new ArrayList<>();
lst.add(3);
lst.add(1);
lst.add(3);
// Removes all of the elements from lst
lst.clear();
assertThat(lst).isEmpty();
}
Conclusion
In this tutorial, we learned to append, insert, update, remove, retain and clear elements from an ArrayList in Java. You can find below the full source code