java中数组倒叙复制输出


public class Test6 {
    public static void main(String[] args) {
        //方法一:新建数组倒叙存储
        /*
        int [] arry = {1,2,3,4,5};
        int [] newArry = new  int [arry.length];
        for (int i = 0,j = arry.length-1; i < arry.length; i++,j--) {
            newArry[i] = arry[j];
        }
        arry = newArry;
        for (int i = 0; i < arry.length; i++) {
            System.out.println("i = " + arry[i]);
        }

         */

        //方法二:运用算法,首位交换
        int [] arry = {1,2,3,4,5};
        for (int i = 0; i < arry.length/2; i++) {
            int temp = arry[i];
            arry[i] = arry[arry.length-i-1];
            arry[arry.length-i-1] = temp;
        }
        for (int i = 0; i < arry.length; i++) {
            System.out.println("arry = " + arry);
        }

    }
}

你可能感兴趣的:(java小练习,java)