How do I reverse an int array in Java?

前端 未结 30 2442
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  旧时难觅i
    2020-11-21 07:44

    Wouldn't doing it this way be much more unlikely for mistakes?

        int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] temp = new int[intArray.length];
        for(int i = intArray.length - 1; i > -1; i --){
                temp[intArray.length - i -1] = intArray[i];
        }
        intArray = temp;
    

提交回复
热议问题