4

If I have an array:

int[] myArray = new int[10]
for (int i = 0; i < myArray.length; i++) {
    myArray[i] = i;
}

//resulting array: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

How can I move everything behing the 4 up one space, and send the 4 to the back? Example:

this:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

into this:

{0, 1, 2, 3, 5, 6, 7, 8, 9, 4}
0

3 Answers 3

4

How about this:

int[] myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.arraycopy(myArray, 5, myArray, 4, 5);
myArray[myArray.length-1] = 4;

In the above code, I'm using the arraycopy method to copy a range of 5 numbers starting from index 5, to index 4 in the array, and then simply set a 4 in the last position.

Notice that using arraycopy is much faster than copying the values in a loop, since it's usually implemented as a native operation which copies memory positions.

EDIT :

A more generic solution, a method for sending to the back a given position in the array:

public static void sendBack(int[] array, int idx) {
    int value = array[idx];
    System.arraycopy(array, idx+1, array, idx, array.length-idx-1);
    array[array.length-1] = value;
}

For your example, call it like this:

sendBack(myArray, 4);
// now myArray is {0, 1, 2, 3, 5, 6, 7, 8, 9, 4}
Sign up to request clarification or add additional context in comments.

Comments

2

Like this?

int start = 4;
int temp = myArray[start];
for(int i = start; i < myArray.length - 1; i++) {
    myArray[i] = myArray[i+1];
}
myArray[myArray.length-1] = temp;

It's the fastest way I can guess...

Comments

1

Use System.arraycopy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.