How do I reverse an int array in Java?

前端 未结 30 2353
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 07:18

I am trying to reverse an int array in Java.

This method does not reverse the array.

for(int i = 0; i < validData.length; i++)
{
    int temp =          


        
30条回答
  •  终归单人心
    2020-11-21 08:09

    There are some great answers above, but this is how I did it:

    public static int[] test(int[] arr) {
    
        int[] output = arr.clone();
        for (int i = arr.length - 1; i > -1; i--) {
            output[i] = arr[arr.length - i - 1];
        }
        return output;
    }
    

提交回复
热议问题