Java ArrayList remove() method with example

Java ArrayList remove() method removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Method Signature

public E remove(int index)

Specified by:

remove method in interface List

Overrides

Overrides:remove method in class AbstractList

Parameters:

index – the index of the element to be removed

Returns:

the element that was removed from the list

Throws Exception:

IndexOutOfBoundsException – if the index is out of range (index = size())

ArrayList remove() method Implementation

public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

Java ArrayList remove() method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;

public class RemoveExample {

	public static void main(String[] args) {

		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>();

		// add() method to add Integer element in the list
		numericList.add(10);
		numericList.add(15);
		numericList.add(20);
		numericList.add(25);
		numericList.add(30);

		// print the values of list
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
		}

		numericList.remove(1);

		System.out.println("List After value removed : ");
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
		}

		// create empty array list with String type
		ArrayList<String> stringList = new ArrayList<String>();

		// add() method to add String element in the list
		stringList.add("Welcome");
		stringList.add("To");
		stringList.add("Java");
		stringList.add("ArrayList");

		// print the values of list
		for (String stringValues : stringList) {
			System.out.println("Strings : " + stringValues);
		}

		// removes by index 2 which remove Java
		stringList.remove(2);
		stringList.remove("ArrayList");

		// Again trying to remove Java
		stringList.remove("Java");

		// trying to remove java which is not exist
		stringList.remove("java");

		System.out.println("List After value removed : ");
		for (String stringValues : stringList) {
			System.out.println("Strings : " + stringValues);
		}
		// We cant remove numeric list based on value it will point to index
		numericList.remove(10);

	}

}

Output

Numbers : 10
Numbers : 15
Numbers : 20
Exception in thread “main” Numbers : 25
Numbers : 30
List After value removed :
Numbers : 10
Numbers : 20
Numbers : 25
Numbers : 30
Strings : Welcome
Strings : To
Strings : Java
Strings : ArrayList
List After value removed :
Strings : Welcome
Strings : To
java.lang.IndexOutOfBoundsException: Index: 10, Size: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.remove(ArrayList.java:496)
at com.ehowtonow.java.util.arraylist.RemoveExample.main(RemoveExample.java:60)


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList add(E e) method with example
  2. Java ArrayList clone() method with example
  3. Java ArrayList addAll(Collection< ? extends E> c) method with example
  4. Java ArrayList get() method with example
  5. Java ArrayList remove() method with example
  6. Java ArrayList add(int index, E element) method with example
  7. Java ArrayList contains() method with example
  8. Java ArrayList addAll(int index, Collection c) method with example
  9. Java ArrayList remove(Object o) method with example
  10. Java ArrayList clear() method with example
  11. Java ArrayList removeAll() method with example
  12. Java ArrayList forEach() method with example
  13. Java ArrayList ensureCapacity() method with example
  14. Java ArrayList indexOf() method with example
  15. Java ArrayList isEmpty() method with example

Be the first to comment

Leave a Reply