How do I reverse an int array in Java?

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

        public static void main(String args[])    {
            int [] arr = {10, 20, 30, 40, 50}; 
            reverse(arr, arr.length);
        }
    
        private static void reverse(int[] arr,    int length)    {
    
            for(int i=length;i>0;i--)    { 
                System.out.println(arr[i-1]); 
            }
        }
    

提交回复
热议问题