beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java ArrayList remove(int index) Method example

By Chaitanya Singh | Filed Under: Java Collections

Method remove(int index) is used for removing an element of the specified index from a list. It removes an element and returns the same. It throws IndexOutOfBoundsException if the specified index is less than zero or greater than the size of the list (index size of ArrayList).

public Object remove(int index)

Example

package beginnersbook.com;
import java.util.ArrayList;
public class RemoveExample {
   public static void main(String args[]) {
       //String ArrayList
       ArrayList<String> al = new ArrayList<String>();
       al.add("AB");
       al.add("CD");
       al.add("EF");
       al.add("GH");
       al.add("AB");
       al.add("YZ");
       System.out.println("ArrayList before remove:");
       for(String var: al){
            System.out.println(var);
       }
       //Removing 1st element
       al.remove(0);
       //Removing 3rd element from the remaining list
       al.remove(2);
       //Removing 4th element from the remaining list
       al.remove(2);
       System.out.println("ArrayList After remove:");
       for(String var2: al){
             System.out.println(var2);
       }
    }
}

Output:

ArrayList before remove:
AB
CD
EF
GH
AB
YZ
ArrayList After remove:
CD
EF
YZ

Enjoyed this post? Try these related posts

  1. Java – Remove all mappings from HashMap example
  2. Java – Check if a particular value exists in HashMap example
  3. How to synchronize ArrayList in java with example
  4. How to convert LinkedList to array using toArray() in Java
  5. Replace Vector elements using index – Java example
  6. Java – Get size of HashMap example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

ArrayList Tutorial

  • Java ArrayList

Java ArrayList Methods

  • add(Object obj)
  • add(int index, Object element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • contains()
  • get()
  • indexOf()
  • ensureCapacity()
  • isEmpty()
  • lastIndexOf()
  • remove()
  • remove(Object obj)
  • trimToSize()
  • set()
  • clone()
  • clear()
  • size()

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap