add(index) Method : Java.util.ArrayList
Declaration :
1 |
public void add(int index, E element) |
Explanation :
Purpose | Method inserts the specified element E at the specified position in this list. |
Parameters | index ==> The index at which the specified element is to be inserted. |
element ==> The element to be inserted | |
Return Value | NA |
Exception | NA |
Example
The following example shows the usage of java.util.Arraylist.add(E) method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.c4learn.arraylist; import java.util.ArrayList; public class ArrayListExamples { public static void main(String[] args) { // Create ArrayList ArrayList<String> arrList1 = new ArrayList<String>(); arrList1.add("Vehicle 1"); arrList1.add("Vehicle 2"); arrList1.add("Vehicle 3"); arrList1.add("Vehicle 4"); // Display Array List System.out.println(arrList1); // Insert at Index arrList1.add(0, "Vehicle 0"); // Display Array List System.out.println(arrList1); } } |
Output :
1 |
[Vehicle 0, Vehicle 1, Vehicle 2, Vehicle 3, Vehicle 4] |