w3resource

Java ArrayList.remove(int index) Method

public E remove(int index)

The remove() method is used to remove an element at a specified index from ArrayList. Shifts any subsequent elements to the left (subtracts one from their indices).

Package: java.util

Java Platform: Java SE 8

Syntax:

remove(int index)

Parameters:

Name Description Type
index The index of the element to be removed. int

Return Value:
The element at the position next to the removed element.

Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

Pictorial presentation of ArrayList.remove() Method

Java ArrayList.remove() Method

Example: ArrayList.remove(int index) Method

The following example creates an ArrayList with a capacity of 7 elements. After adding 7 elements we have removed two elements form 2nd and 5th position.

import java.util.*;

public class test {
   public static void main(String[] args) {
      
    // create an empty array list with an initial capacity
    ArrayList<String> color_list = new ArrayList<String>(7);

    // use add() method to add values in the list
    color_list.add("White");
    color_list.add("Black");
    color_list.add("Red");
    color_list.add("White");
	color_list.add("Yellow");
	color_list.add("Red");
    color_list.add("White");
  
	// Print out the colors in the ArrayList
    System.out.println("****Color list****");
	for (int i = 0; i < 7; i++)
      {
         System.out.println(color_list.get(i).toString());
      }

    // Removes element at 3rd position
    color_list.remove(2);
	
	//New size of the list
	System.out.println("New Size of list: " + color_list.size()); 
	
	// Fresh Color list
    System.out.println("****Fresh Color list****");
	for (int i = 0; i < 6; i++)
      {
         System.out.println(color_list.get(i).toString());
      }
  }
} 
  

Output:

F:\java>javac test.java

F:\java>java test
****Color list****
White
Black
Red
White
Yellow
Red
White
Now, Size of list: 6
****Fresh Color list****
White
Black
White
Yellow
Red
White

Example of Throws: remove(int index) Method

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

Let

int index = str.remove(8);

in the above example.

Output:

****Color list****                                     
White                                                  
Black                                                  
Red                                                    
White                                                  
Yellow                                                 
Red                                                    
White                                                  
New Size of list: 6                                    
****Fresh Color list****                               
White                                                  
Black                                                  
White                                                  
Yellow                                                 
Red                                                    
White                                                  
Exception in thread "main" java.lang.IndexOutOfBoundsEx
ception: Index: 6, Size: 6                             
        at java.util.ArrayList.rangeCheck(ArrayList.jav
a:635)                                                 
        at java.util.ArrayList.get(ArrayList.java:411) 
        at test.main(test.java:35) 

Java Code Editor:

Previous:add(int index, E element) Method
Next:remove(Object o) Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/arraylist/arraylist_remove.php