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 tha
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]
Actually the way to do it on your specific question is arrayList.add(1,"INSERTED ELEMENT");
where 1 is the position
For example:
I want to move the element from 23th to 1th(index == 0) in the arrayList, so I put 23th element to a temp value, and removing from list, insert it to 1th in list. It's worked, but not more efficient.
List<ItemBean> list = JSON.parseArray(channelJsonStr,ItemBean.class);
for (int index = 0; index < list.size(); index++) {
if (list.get(index).getId() == 23) { // id 23
ItemBean bean = list.get(index);
list.remove(index);
list.add(0, bean);
}
}
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.
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.
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)