You can use this ArrayList class method to insert an item at a specific location in an ArrayList

Suppose I have an ArrayList of objects of size n. Now I want to insert an another object at specific position, let's say at index position k (is greater than 0 and less than n) and I want other objects at and after index position k to shift one index position ahead. So is there any way to do this directly in Java. Actually i want to keep the list sorted while adding new object.

Bohemian

397k89 gold badges558 silver badges699 bronze badges

asked Aug 16, 2011 at 6:34

You can use this ArrayList class method to insert an item at a specific location in an ArrayList

Harshveer SinghHarshveer Singh

3,8976 gold badges35 silver badges45 bronze badges

To insert value into ArrayList at particular index, use:

public void add(int index, E element)

This method will shift the subsequent elements of the list. but you can not guarantee the List will remain sorted as the new Object you insert may sit on the wrong position according to the sorting order.


To replace the element at the specified position, use:

public E set(int index, E element)

This method replaces the element at the specified position in the list with the specified element, and returns the element previously at the specified position.

answered Aug 16, 2011 at 6:36

CloudyMarbleCloudyMarble

36.3k70 gold badges95 silver badges128 bronze badges

5

Here is the simple arraylist example for insertion at specific index

ArrayList<Integer> str=new ArrayList<Integer>();
    str.add(0);
    str.add(1);
    str.add(2);
    str.add(3); 
    //Result = [0, 1, 2, 3]
    str.add(1, 11);
    str.add(2, 12);
    //Result = [0, 11, 12, 1, 2, 3]

answered Jan 3, 2014 at 12:48

You can use this ArrayList class method to insert an item at a specific location in an ArrayList

Jaldip KatreJaldip Katre

2,9161 gold badge18 silver badges14 bronze badges

1

Note that when you insert into a List at a position, you are really inserting at a dynamic position within the List's current elements. See here:

http://tpcg.io/0KmArS

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {

      // create an empty array list with an initial capacity
      ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

      // use add() method to add elements in the list
      arrlist.add(15, 15);
      arrlist.add(22, 22);
      arrlist.add(30, 30);
      arrlist.add(40, 40);

      // adding element 25 at third position
      arrlist.add(2, 25);

      // let us print all the elements available in list
      for (Integer number : arrlist) {
         System.out.println("Number = " + number);
      }  
   }
}

$javac com/tutorialspoint/ArrayListDemo.java

$java -Xmx128M -Xms16M com/tutorialspoint/ArrayListDemo

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 0
    at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:661)
    at java.util.ArrayList.add(ArrayList.java:473)
    at com.tutorialspoint.ArrayListDemo.main(ArrayListDemo.java:12)

answered Jul 31, 2018 at 14:13

You can use this ArrayList class method to insert an item at a specific location in an ArrayList

1

From Oracle Official Documentation

This method Appends the specified element to the end of this list.

add(E e) //append element to the end of the arraylist.

This method Inserts the specified element at the specified position in this list.

void add(int index, E element) //inserts element at the given position in the array list.

This method Replaces the element at the specified position in this list with the specified element.

set(int index, E element) //Replaces the element at the specified position in this list with the specified element.
      

      

answered Sep 29, 2020 at 8:11

You can use this ArrayList class method to insert an item at a specific location in an ArrayList

1

Actually the way to do it on your specific question is arrayList.add(1,"INSERTED ELEMENT"); where 1 is the position

answered May 31, 2013 at 5:34

You must handle ArrayIndexOutOfBounds by yourself when adding to a certain position.

For convenience, you may use this extension function in Kotlin

/**
 * Adds an [element] to index [index] or to the end of the List in case [index] is out of bounds
 */
fun <T> MutableList<T>.insert(index: Int, element: T) {
    if (index <= size) {
        add(index, element)
    } else {
        add(element)
    }
}

answered Nov 14, 2020 at 3:48

You can use this ArrayList class method to insert an item at a specific location in an ArrayList

Leonid UstenkoLeonid Ustenko

13.9k3 gold badges60 silver badges54 bronze badges

Which of the following ArrayList class methods is used to insert an item at a specific?

Methods in Java ArrayList.

How do I add an item to an ArrayList in Java?

For example, to add elements to the ArrayList , use the add() method:.
import java. util. ... .
public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars. add("Volvo"); cars. ... .
Create an ArrayList to store numbers (add elements of type Integer ): import java. util..

Which method is used to determine the number of items stored in an ArrayList object?

You can use the size() method of java. util. ArrayList to find the length or size of ArrayList in Java. The size() method returns an integer equal to a number of elements present in the array list.

What is retrieval operation in ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method. This method has a single parameter i.e. the index of the element that is returned.