To reverse an integer array we swap items until we reach the midpoint as shown below
public int [] ReverseIntArray() { int[] arr = { 1, 5, 20 }; for(int i= 0;i< (arr.Length)/2;i++) { int temp = arr[i]; //Keep traversing from last arr[i] = arr[arr.Length - 1-i]; arr[arr.Length - 1 - i] = temp; } return arr; }
No comments:
Post a Comment