4

Here is my array:

int[] myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

Let's say I want to move myArray[3] (it could be any element) and myArray[6] (same with this one) to the front of the array while rearranging the back, how can I do this? Example:

This:

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

Into this:

{3, 6, 0, 1, 2, 4, 5, 7, 8, 9}
1
  • 2
    @ Conner: Try to use linked list concept for this problem.Rearranging the array will take lot of time and space complexity. Commented Dec 20, 2011 at 7:29

2 Answers 2

4

To move index x to the front, you need to:

  • Remember what's at index x
  • Copy everything from 0 to x - 1 up one index, e.g. with System.arrayCopy
  • Set the value at index 0 to be the value you remembered in the first step

For example:

public void moveToHead(int[] values, int index)
{
    // TODO: Argument validation
    int value = values[index];
    System.arraycopy(values, 0, values, 1, index - 1);
    values[0] = value;
}

Note that System.arraycopy handles the copying appropriately:

If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.

Your original example mentioned two elements - while you could potentially do all of this more efficiently knowing both elements in advance, it would be far, far simpler to model this as two moveToHead calls. You need to take care about the ordering - for example, if you want to move index 6 to the head first, you'll need to then move index 4 rather than index 3, to take account of the first move.

Sign up to request clarification or add additional context in comments.

4 Comments

@ConnerRuhl: I've provided a method to do the move - now you just need to call it with appropriate levels of care. (See my last paragraph.)
When I try this with objects it overrides objects with references
@ConnerRuhl: You'll need to provide more details than that - such as a short but complete program demonstrating the problem.
@ConnerRuhl: Well you can never have an array of objects - you can only have an array of references. But then yes, it should work.
2

An other solution may be to transform your array into a list thanks to the asList method and simply use the remove and add methods:

List<Integer> myList = new ArrayList<Integer>(Arrays.asList(myArray));
myList.add(myList.remove(myIndex));

2 Comments

Unfortunately, this won't work - the list you get from Arrays.asList allows neither add nor remove. Replace it with new ArrayList<Integer>(Arrays.asList(myArray)) and it should work, though.
I was wondering about that but forgot to test it. Thanks for the comment, the answer is corrected :)

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.