Open In App

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

Last Updated : 26 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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)



Similar Reads

TimeZone getOffset(int, int, int, int, int, int) Method in Java with Examples
The getOffset(int era, int yr, int mon, int day, int dayOfWeek, int millisec) method of TimeZone class in Java is used to know the offset value of this TimeZone at a specific date or modified date in case of daylight savings, from the UTC or the Universal Time Coordinated. This offset value can be added to get the local time. Syntax: public abstrac
2 min read
ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
The ofHoursMinutesSeconds(int, int, int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours, minutes and seconds passed as the parameter. This method takes the hours, minutes and seconds as parameter in the form of int and converts it into the ZoneOffset. Syntax: public static ZoneO
2 min read
SimpleTimeZone setStartRule(int, int, int) method in Java with Examples
The setStartRule(int startofMonth, int startofDay, int startofTime) method of SimpleTimeZone class in Java is used to set a particular start rule of the day-light saving time to a fixed date within a given month. Syntax: public void setStartRule(int startofMonth, int startofDay, int startofTime) Parameters: The method takes three parameters: starto
2 min read
SimpleTimeZone setEndRule(int, int, int) method in Java with Examples
The setEndRule(int endofMonth, int endofDay, int endofTime) method of SimpleTimeZone class in Java is used to set a particular rule of the day-light saving time to a fixed date within a given month. Syntax: public void setEndRule(int endofMonth, int endofDay, int endofTime) Parameters: The method takes three parameters: endofMonth: This is of Integ
2 min read
AbstractList add(E ele) method in Java with Examples
The add(E ele) method of AbstractList class in Java is used to insert the specified element to the end of the current list. Syntax: public boolean add(E ele) Where E is the type of element maintained by this AbstractList collection. Parameter: This method accepts a single parameter ele which represents the element to be inserted at the end of this
2 min read
HijrahDate of(int, int, int) method in Java with Example
The of() method of java.time.chrono.HijrahDate class is used to generate the date according to Islamic hijri calendar system by using the passed proleptic year, month and date. Syntax: public static HijrahDate of(int Year, int month, int dayOfMonth) Parameter: This method takes the following argument as a parameter: year: which is the integer value
2 min read
IsoChronology date(int, int, int) method in Java with Example
The date() method of java.time.chrono.IsoChronology class is used get the local date according to ISO calendar system for the given year, month and day. Syntax: public LocalDate date(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following arguments as parameter. prolepticYear: integer proleptic year for the year fie
2 min read
JapaneseChronology date(int, int, int) method in Java with Example
The date() method of java.time.chrono.JapaneseChronology class is used get the local date according to japanese calendar system for the given year, month and day.Syntax: public JapaneseDate date(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following arguments as parameter. prolepticYear: integer proleptic year for
2 min read
JapaneseDate of(int, int, int) method in Java with Example
The of() method of java.time.chrono.JapaneseDate class is used to generate the date according to japanese calendar system by using the passed proleptic year, month and date. Syntax: public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following argument as a parameter: year: which is the integ
2 min read
JapaneseDate of(JapaneseEra,int, int, int) method in Java with Example
The of() method of java.time.chrono.JapaneseDate class is used to generate the date according to Japanese calendar system by using the passed proleptic year, month, date and JapaneseEra. Syntax: public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) Parameter: This method takes the following argument as a parameter
2 min read
Practice Tags :