Open In App

Java AbstractSequentialList | add()

AbstractSequentialList uses the add() method specified by the List interface and overridden in class AbstractList. Hence AbstractSequentialList do not have a add() method of its own. Instead it has add(int index, E element) method.

AbstractSequentialList add(int index, E element) method inserts the specified element at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). This implementation first gets a list iterator pointing to the indexed element (with listIterator(index)). Then, it inserts the specified element with ListIterator.add.



Therefore, add(E element) and add(int index, E element) both can be used with AbstractSequentialList class.

Syntax



public void add(int index, E element)

Parameters: This method takes two parameters:

Exceptions: This method throws following exceptions:

Below are the programs to illustrate add() method:

Program 1: Adding the elements, by without passing the index




// Java program to demonstrate
// add() method
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<Integer> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add(5);
        absl.add(6);
        absl.add(7);
  
        // Printing the list
        System.out.println(absl);
    }
}

Output:
[5, 6, 7]

Java-CollectionsRemove term: Java-Functions Java-Functions
Program 2: Adding the elements, by passing the index




// Java program to demonstrate
// add() method
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<Integer> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add(0, 8);
        absl.add(1, 7);
        absl.add(1, 9);
        absl.add(3, 10);
  
        // Printing the list
        System.out.println(absl);
    }
}

Output:
[8, 9, 7, 10]

Program 3: To demonstrate IndexOutOfBoundException




// Java code to show IndexOutofBoundException
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
  
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<Integer> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add(5);
        absl.add(6);
        absl.add(7);
        absl.add(2, 8);
        absl.add(2, 7);
        absl.add(1, 9);
        absl.add(4, 10);
  
        // Printing the list
        System.out.println(absl);
  
        try {
            // showing IndexOutOfBoundsException
            absl.add(10, 12);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

Output:
[5, 9, 6, 7, 10, 8, 7]
Exception: java.lang.IndexOutOfBoundsException: Index: 10, Size: 7

Article Tags :