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
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers
- Java ArrayList listIterator() method with example
- Java ArrayList remove() method with example
- Java ArrayList removeRange() method with example
- Java ArrayList set() method with example
- Java ArrayList addAll(int index, Collection extends E> c) method with example
- Java ArrayList get() method with example
- Java ArrayList contains() method with example
- Java ArrayList forEach() method with example
- Java ArrayList indexOf() method with example
- Java ArrayList size() method with example
- Java ArrayList subList() method with example
- Java ArrayList removeAll() method with example
- Java ArrayList add(E e) method with example
- Java ArrayList spliterator() method with example
- Java ArrayList ensureCapacity() method with example
Leave a Reply
You must be logged in to post a comment.