2

Is there any way to move items inside an array? For example:

int[] myArray = {1,2,3,4};

2nd element becomes the last:

int[] myArray = {1,3,4,2};

P.S.: No that's not a homework. I can think of at least one solution but it requires rather difficult implementation:

  • First we save second element to Int
  • then we remove this element from the array
  • then we add new element at the very end of my array

Any other (read - easier) way to do this?

3
  • 6
    Won't swapping the elements around do the same thing? Commented Jun 5, 2012 at 15:20
  • by swapping we mean that the second element becomes the last, but the last becomes the second.. I wanted to move the second element without changing the order of another elements. (as like in example). Commented Jun 5, 2012 at 15:28
  • Note that homework questions are allowed here, as long as you follow the rules as stated in the 'homework' tag's description (and add the 'homework' tag to your question). Commented Jun 5, 2012 at 15:30

5 Answers 5

8

There is no easy way to do it using an array. You'll have to loop through the array shifting every element up to the index that's moving, and then re-insert that element at the end. You could always use a List<int> to do it.

List<int> list = myArray.ToList();
int value = list[1];
list.RemoveAt(1);
list.Add(value);
myArray = list.ToArray();
Sign up to request clarification or add additional context in comments.

Comments

3

Use List<int>. Array is not for such movements of all the elements.

Or try to swap the last and the chosen element, it will mess up the order though.

1 Comment

note that, if you need the array for something later, you just call List<int>.ToArray()
1

If the array is always a fixed size then:

myArray = new int[4] { myArray[0], myArray[2], myArray[3], myArray[1] };

But if it is variable in size then it becomes trickier and is best done in a List first

int lastElement = 0;
List<int> newArray = new List<int>();
for ( int index=0; index<myArray.Length; index++)
{
     if ( index == 1 ) lastElement = myArray[index];
     else newArray.Add(myArray[index]);
}
newArray.Add(lastElement);
myArray = newArray.ToArray();

Comments

1
   myArray = new List<int>(myArray.Where((x, i) => (i != 1))) 
               { myArray[1] }.ToArray();

1 Comment

Thank you for your solution! I've checked it just now and it worked fine! However, please note that this method has some serious performance issues. It's turned out that this approach is almost 10x slower when to compared to (array->list-> removeat->add) approach (210ms vs 2+ seconds) after 1 million of iterations.
0

For this instance, you can use the XOR swap:

[Test]
public void TestSwap()
{
    int[] myArray = { 1, 2, 3, 4 };

    Console.WriteLine(string.Join(", ", myArray));

    Swap(myArray, 1, 2);
    Swap(myArray, 2, 3);

    Console.WriteLine(string.Join(", ", myArray));
}

static void Swap(int[] vals, int x, int y)
{
    vals[x] = vals[x] ^ vals[y];
    vals[y] = vals[y] ^ vals[x];
    vals[x] = vals[x] ^ vals[y];
}

3 Comments

Why would you want to do this? This will actually be slower than using a temporary variable (also will not work if values are identical)
@BrokenGlass: "Is there any way to move items inside an array?" This is a way.
No you are not moving elements - you are swapping them, just like had been suggested already

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.