-1

I’m trying to write a method that returns the reverse of a given array. It accepts an array parameter and then returns the reverse of it. I’m currently a beginner in java and is it possible to be done with a nested for loop

RAVE CLO
  • 14
  • 3
  • There are various ways to do it. I prefer using org.apache.commons.lang3.ArrayUtils.reverse(..) for this. ArrayUtils.reverse() Check other implementation examples : https://www.baeldung.com/java-invert-array – www.hybriscx.com Oct 26 '19 at 05:55

1 Answers1

-1
public static void ReverseArray(int[] intArr)
{
    for(int x = 0, z = intArr.length-1; x < intArr.length /2; x++, z --)
    {
        //intArr = {1,2,3,4,5,6,7,8,9}

        int n1 = intArr[x]; //n1 = 1 
        int n2 = intArr[z]; //n2 = 9

        intArr[x] = n2; //Swap the element at index x with n2.
        intArr[z] = n1; //Swap the element at index z with n1.

       //intArr = {9,2,3,4,5,6,7,8,1}
    }


}
Bradley
  • 327
  • 2
  • 11
  • Please don't post only code as an answer, but include an explanation what your code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Oct 26 '19 at 06:48