The remove() method of AbstractList class in Java

Java 8Object Oriented ProgrammingProgramming

Remove an element at a specified position from the list using the remove() method. The position is to be set as index parameter in the method itself. It returns the element which is removed.

The syntax is as follows:

public E remove(int index)

Here, index is the index from where you want to delete the element.

To work with the AbstractList class, import the following package:

import java.util.AbstractList;

The following is an example to implement remove() method of the AbstractlList class in Java:

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer> myList = new ArrayList<Integer>();
      myList.add(50);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(300);
      myList.add(350);
      myList.add(400);
      System.out.println("Elements in the AbstractList = " + myList);
      System.out.println("Removing the element = " + myList.remove(5));
      System.out.println("Elements in the updated AbstractList = " + myList);
   }
}

Output

Elements in the AbstractList = [50, 100, 150, 200, 250, 300, 350, 400]
Removing the element = 300
Elements in the updated AbstractList = [50, 100, 150, 200, 250, 350, 400]
raja
Published on 02-Apr-2019 12:22:35
Advertisements