?
-
I have an array, eg. int[] x = {1, 2, 3, 4};
I want to relocate the index such that relocate(fromIndex, toIndex)
using values relocate(3, 1) will change the array to {1, 4, 2, 3}.
The following is a working code:
int movedX = x[fromIndex];
System.arraycopy(x, 0, x, 0, fromIndex);
int index = fromIndex + 1;
System.arraycopy(x, index, x, fromIndex, x.length - index);
System.arraycopy(x, 0, x, 0, toIndex);
System.arraycopy(x, toIndex, x, toIndex + 1, x.length - toIndex - 1);
x[toIndex] = movedX;
Is there a shorter way to do it?
(Using the classes in the java.util.* package is not an option.)
I want to relocate the index such that relocate(fromIndex, toIndex)
using values relocate(3, 1) will change the array to {1, 4, 2, 3}.
The following is a working code:
int movedX = x[fromIndex];
System.arraycopy(x, 0, x, 0, fromIndex);
int index = fromIndex + 1;
System.arraycopy(x, index, x, fromIndex, x.length - index);
System.arraycopy(x, 0, x, 0, toIndex);
System.arraycopy(x, toIndex, x, toIndex + 1, x.length - toIndex - 1);
x[toIndex] = movedX;
Is there a shorter way to do it?
(Using the classes in the java.util.* package is not an option.)