Java ArrayList insert element at beginning example shows how to insert element at beginning of ArrayList in Java. The example also shows how to insert element at specified index in ArrayList.
How to insert an element at beginning of the ArrayList in Java?
When we add elements to the ArrayList using the add
method, they are inserted at the end of the ArrayList. To insert element at the beginning or start of the ArrayList, use the overloaded add
method of ArrayList which also accepts an index parameter where the element needs to be inserted.
1 |
public void add(int index, E element) |
This method inserts the element at the specified index in the ArrayList. If there is any element exists at the specified position, it shifts the existing elements along with any subsequent elements to the right (means increases their index by 1).
Java ArrayList insert an element at the beginning example
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 26 27 28 29 30 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListInsertElementExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //we are going to add some numbers to ArrayList aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); /* * To insert element at beginning of ArrayList * use add method of ArrayList class with index 0 */ aListNumbers.add(0, "Zero"); System.out.println("Element added at beginning of ArrayList"); //print ArrayList System.out.println(aListNumbers); } } |
Output
1 2 |
Element added at beginning of ArrayList [Zero, One, Two, Three] |
As you can see from the output, we specified index as 0 in the add
method to insert element at the start of the ArrayList. List automatically shifted all the existing elements by increasing their index by 1.
How to insert an element at specified index in ArrayList?
Use the add
method with the index you want to insert an element in the ArrayList. All subsequent elements (if any) will be shifted right automatically by ArrayList.
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 26 27 28 29 30 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListInsertElementExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //add some elements to ArrayList aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); /* * To insert element at specified index * use add method of ArrayList class with index */ aListNumbers.add(2, "2.5"); System.out.println("Element inserted at specified index"); //print ArrayList System.out.println(aListNumbers); } } |
Output
1 2 |
Element inserted at specified index [One, Two, 2.5, Three] |
You might want to check if the element already exists in the ArrayList before adding it. Check out this example if you want to replace elements in ArrayList.
This example is a part of the Java ArrayList tutorial with examples.
Please let me know your views in the comments section below.
Except it does not actually add a new element to the array, nor does it shift the other elements up by one. All it does is modifies the value of the specified element.
Hello Bryan,
Thanks for the comment. However, I think you might have confused the ArrayList set method with the add method. The ArrayList add method actually adds a new element and shifts other elements, if required, as shown in the example. Your comment fits the description of the set method which replaces the element at specified index.
I hope it helps.
Hi Bryan,
Add method inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Attached is the link for your reference:
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E-