1

Imagine something like this: I have an array of 4 elements and I want to exchange the value between the element 1 and 3. How can I do this?

int[] i = new array[4];

i[0] = 10;
i[1] = 20;
i[2] = 30;
i[3] = 40;

if(something...)
{
  i[1] = i[3]
  i[3] = previous value of i[1]
}

I want this:

i[0] = 10;
i[1] = 40;
i[2] = 30;
i[3] = 20;

ANy idea?

Sanket
  • 19,295
  • 10
  • 71
  • 82

1 Answers1

1

By using a temporary variable.

if(something...)
{
  var temp = i[1];
  i[1] = i[3];
  i[3] = temp;
}
samvdst
  • 618
  • 7
  • 22