CopperCleric
CopperCleric

Reputation: 83

Problems of returning an array

 public static void main(String[] args) {
   int [] arraynum = {1,2,3,4,5,6,7,8,9,10};
   reversearray(arraynum);                     
 }

I am trying to reverse the array

public static int reversearray(int[] num){
   int n = num.length;
   
   for (int i = 1 ; i<=n/2 ; i++){
       int temp = num[i];
       num[i] = num[n];
       num[n] = temp;
       n--;             
   }
   
  

How to return the reverse array?

Upvotes: 0

Views: 25

Answers (1)

Promila Ghosh
Promila Ghosh

Reputation: 389

To reverse the array, you need to iterate in opposite direction.

while (start < end)
        {
            temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }

Upvotes: 1

Related Questions