Thursday, May 14, 2026
HomeLanguagesJavaAbstractList add(int index, E element) method in Java with Examples

AbstractList add(int index, E element) method in Java with Examples

The add(int index, E element) method of AbstractList is used to insert an element at the specified position in this list. The new element is inserted at the specified index in this list.

Syntax:

public void add(int index, E element)

Parameters: This method takes 2 parameters :-

  1. index: index at which the specified element is to be inserted
  2. element: element to be inserted

Returns: The function returns a boolean value True if the element is successfully inserted in the List otherwise it returns False.

Exceptions:The exceptions present are :-

  • ClassCastException: if the class of an element of this set is incompatible with the specified collection
  • NullPointerException: if this set contains a null element and the specified collection does not permit null elements, or if the specified collection is null.
  • UnsupportedOperationException: if the add operation is not supported by this list.
  • IllegalArgumentException: if some property of the specified element prevents it from being added to this list.
  • IndexOutOfBoundsException: if the index is out of range (index size()).

Below program illustrates the add(int index, int position) function of AbstractList class :

Program 1:




// Java code to illustrate AbstractList
// add(int index, int position) method
  
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
    public static void main(String[] args)
    {
  
        // Create an empty list
        // with an initial capacity
        AbstractList<Integer> list
            = new ArrayList<Integer>(5);
  
        // Use add() method
        // to add elements in the list
        list.add(3);
        list.add(6);
        list.add(9);
        list.add(12);
  
        // Prints all the elements
        // available in list
        System.out.println("Before: "
                           + list);
  
        // Add(int index, int element) method
        list.add(1, 4);
  
        // Prints all the elements
        // after the above method
        System.out.println("After: "
                           + list);
    }
}


Output:

Before: [3, 6, 9, 12]
After: [3, 4, 6, 9, 12]

Program 2:




// Java code to illustrate AbstractList
// add(int index, int position) method
  
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
    public static void main(String[] args)
    {
  
        // Create an empty list
        // with an initial capacity
        AbstractList<String> list
            = new ArrayList<String>(5);
  
        // Use add() method
        // to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Computer");
        list.add("Portal");
  
        // Prints all the elements
        // available in the list
        System.out.println("Before: " + list);
  
        // Add(int index, int element) method
        list.add(2, "Geeks");
  
        // Prints all the elements
        // after the above method
        System.out.println("After: " + list);
    }
}


Output:

Before: [Geeks, for, Computer, Portal]
After: [Geeks, for, Geeks, Computer, Portal]

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractList.html#add(int, %20E)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS