Reputation: 83
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
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