I need to insert a element of type Person (my own defined class) in an ArrayList at index i
I know I can use add(int index, E element)
.
But is there any efficient method to do this as in my list it is taking around 1.5 ms on an average (data collected over 1000 insertion and then average).
The java. util. ArrayList. add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object. Add the required element to the array list.
add() method is used to add an element at particular index in Java ArrayList.
If your task is more insertion / deletion intensive, you can always use java.util.LinkedList.
For LinkedList
:
For ArrayList
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With