Quick insert in an Array of primitives(int) in Java, I know the size
I am a bit desperate with this, but I don't know if I can do it in a better way. I can get 100.000 inserts in a bit more than 5 seconds in my computer, using System.arrayCopy, I just copy the array a place to the right and modify the value in the element where it needs to be inserted. Using a LinkedList it takes around 66 secons.
The method that I use:
// the size is known, and never will insert more elements than the size of the array
/**
* @param ar is the array
* @param indexO is the index of the insert
* @param indexD is the insert limit to be pushed right
*/
public static final void insert(int[]ar,int indexO,int indexD, int newValue){
System.arraycopy(ar,indexO,ar,indexO+1,indexD-indexO);
ar[indexO]=newValue;
}
Any advice about how this could be speed up, if possible
Note added:
I tried using this inserting in different arrays, I fill it by inserting always in the first element:
100k elements, +-5seconds.
120k elements +- 8 seconds.
140k elements +- 12 seconds.
160k elements +- 23 seconds.
180k elements +- 37 seconds.
200k elements +- 52 seconds.