Example
Insert an item into a list.
|
The add() method inserts an item into the list.
If an index is specified, the new item will be positioned at that index, pushing all subsequent elements in the list ahead by one.
If no index is provided, the new item will be added to the end of the list.
Choose one from the following:
public boolean add(T item) |
public void add(int index, T item) |
T denotes the data type of elements in the list.
Parameter |
Description |
index |
Optional. The position in the list at which to add the item. |
item |
Required. The item to be added to the list. |
Returns: |
Nothing if an index is specified. When an index is not specified it returns |
Throws: |
IndexOutOfBoundsException occurs if the index is negative or exceeds the list’s size. |
Example
Insert an item at a designated position within the list.
|