In this Java ArrayList tutorial, you will learn how to remove elements from ArrayList in Java. There are actually two methods to remove an existing element from ArrayList, first by using
remove(int index) method, which removes elements with given index, remember index starts with zero in ArrayList. So a call to remove(2) in an ArrayList of
{"one", "two", "three"} will remove 3rd element which is "three". Second method to remove element is r
emove(Object obj), which removes given object from ArrayList. For example a call to
remove("two") will remove the second element form ArrayList. Though you should remember to use Iterator or ListIterator remove() method to delete elements while iterating, using ArrayList's remove methods in that case will throw
ConcurrentModificationException in Java. Things get little complicated when you are working with ArrayList of integral numbers e.g. ArrayList of integers. If your list contains numbers which are same as indexes e.g. then a call to
remove(int index) can be confused with a call to
remove(Object obj), for example, if you have an ArrayList containing
{1, 2, 3} then a call to
remove(2) is ambiguous, because it could be interpreted a call to remove 2, which is second element or a call to remove element at index 2, which is actually 3. If you want to learn more about Collection classes, I suggest you to take a look at one of the all time classic book,
Java Generics and Collection. This is the book I refer to refresh my knowledge on the topic.