How do I reverse an int array in Java?

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

    static int[] reverseArray(int[] a) {
         int ret[] = new int[a.length];
         for(int i=0, j=a.length-1; i=0; i++, j--)
             ret[i] = a[j];
         return ret;
    }
    

提交回复
热议问题