torsdag 31 maj 2012

Swapping values using arrays

private void SwapElements (int [ ] arrayName; int position1, int position2){
   int temporary = arrayName[position1];
   arrayName[position1] = arrayName[position2];
   arrayName[position2] = temporary;
}

You have to send (to the method) the actual array and the positions of the elements in it that you want to swap. If you send only the elements (int arrayName[1], int arrayName[3]) you are only sending a COPIE and it will change nothing in the array itself.