How do I reverse an int array in Java?

前端 未结 30 2440
爱一瞬间的悲伤
爱一瞬间的悲伤 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 07:53

    Simple for loop!

    for (int start = 0, end = array.length - 1; start <= end; start++, end--) {
        int aux = array[start];
        array[start]=array[end];
        array[end]=aux;
    }
    

提交回复
热议问题