How do I reverse an int array in Java?

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

    public void getDSCSort(int[] data){
            for (int left = 0, right = data.length - 1; left < right; left++, right--){
                // swap the values at the left and right indices
                int temp = data[left];
                data[left]  = data[right];
                data[right] = temp;
            }
        }
    

提交回复
热议问题