How do I reverse an int array in Java?

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

    This will help you

    int a[] = {1,2,3,4,5};
    for (int k = 0; k < a.length/2; k++) {
        int temp = a[k];
        a[k] = a[a.length-(1+k)];
        a[a.length-(1+k)] = temp;
    }
    

提交回复
热议问题