Sogrey / Android_QA

https://sogrey.github.io/Android_QA/
MIT License
0 stars 0 forks source link

有数组a[n],用java代码将数组元素顺序颠倒 #130

Open Sogrey opened 5 years ago

Sogrey commented 5 years ago
import java.util.Arrays;

public class SwapDemo{

    public static void main(String[] args){
        int [] a = new int[]{
                        (int)(Math.random() * 1000),
                        (int)(Math.random() * 1000),
                        (int)(Math.random() * 1000),
                        (int)(Math.random() * 1000),                        
                        (int)(Math.random() * 1000)                                                                     
        };  

        System.out.println(a);
        System.out.println(Arrays.toString(a));
        swap(a);
        System.out.println(Arrays.toString(a));     
    }

    public static void swap(int a[]){
        int len = a.length;
        for(int i=0;i<len/2;i++){
            int tmp = a[i];
            a[i] = a[len-1-i];
            a[len-1-i] = tmp;
        }
    }
}