Reversing an array GFG
package com.company;
import java.util.Arrays;
public class Array5 {
// Reverse an array
public static int[] getReverse(int[] arr) {
int l = arr.length;
for (int i = 0; i < Math.floorDiv(l, 2); i++) {
int a = arr[i];
arr[i] = arr[l - 1 - i];
arr[l - 1 - i] = a;
}
return arr;
}
public static void main(String[] args) {
int[] array = {2, 4, 5, 6, 8, 9, 0, 89, 78};
System.out.println((Arrays.toString(getReverse(array))));
}
}
Comments
Post a Comment