数组的拷贝与排序

四种拷贝都是浅拷贝:
for循环;
object.clone 会产生新对象; native方法
System.arraycopy(src,srcpos,dest,destpos,length)不产生新对象 底层调用native
Arrays.copyOf ,产生新对象 返回值T[] 底层调用System.arraycopy;

class TestArray2 {
    private int val;
    public void setVal(int val) {
        this.val = val;
    }
    public int getVal() {
        return this.val;
    }
}

public class TestDemo2 {

    public static void main(String[] args) {
        TestArray2[][] testArray2 = new TestArray2[2][2];
        testArray2[0][0] = new TestArray2();
        testArray2[0][1] = new TestArray2();
        testArray2[1][0] = new TestArray2();
        testArray2[1][1] = new TestArray2();
        TestArray2[][] testArray3 = new TestArray2[2][2];
       // System.arraycopy();
        /*for (int i = 0; i < testArray2.length; i++) {
            testArray3[i] = Arrays.copyOf(testArray2[i],testArray2[i].length);
            //System.arraycopy(testArray2[i],0,testArray3[i],0,
                    //testArray2[i].length);
        }*/
        System.out.println(testArray3);
        System.out.println(testArray2);
        testArray3 = Arrays.copyOf(testArray2,testArray2.length);
        System.out.println(testArray3);
        System.out.println(testArray2);


        System.out.println("=============拷贝完成=========");
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                System.out.print(testArray2[i][j].getVal()+" ");
            }
        }
        System.out.println();
        for (int i = 0; i < testArray3.length; i++) {
            for (int j = 0; j < testArray3[i].length; j++) {
                System.out.print(testArray3[i][j].getVal()+" ");
            }
        }
        System.out.println();

        testArray2[0][0].setVal(1000000);
        System.out.println("============修改完成=========");
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                System.out.print(testArray2[i][j].getVal()+" ");
            }
        }
        System.out.println();
        for (int i = 0; i < testArray3.length; i++) {
            for (int j = 0; j < testArray3[i].length; j++) {
                System.out.print(testArray3[i][j].getVal()+" ");
            }
        }
        System.out.println();

    }


    public static void main4(String[] args) {
        TestArray2[][] testArray2 = new TestArray2[2][2];
        testArray2[0][0] = new TestArray2();
        testArray2[0][1] = new TestArray2();
        testArray2[1][0] = new TestArray2();
        testArray2[1][1] = new TestArray2();
        TestArray2[][] testArray3 = new TestArray2[2][2];
        //clone
        for (int i = 0; i <testArray2.length ; i++) {
            testArray3[i] = testArray2[i].clone();
        }
        System.out.println("=============拷贝完成=========");
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                System.out.print(testArray2[i][j].getVal()+" ");
            }
        }
        System.out.println();
        for (int i = 0; i < testArray3.length; i++) {
            for (int j = 0; j < testArray3[i].length; j++) {
                System.out.print(testArray3[i][j].getVal()+" ");
            }
        }
        System.out.println();

        testArray2[0][0].setVal(1000000);
        System.out.println("============修改完成=========");
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                System.out.print(testArray2[i][j].getVal()+" ");
            }
        }
        System.out.println();
        for (int i = 0; i < testArray3.length; i++) {
            for (int j = 0; j < testArray3[i].length; j++) {
                System.out.print(testArray3[i][j].getVal()+" ");
            }
        }
        System.out.println();

    }

    public static void main3(String[] args) {
        TestArray2[][] testArray2 = new TestArray2[2][2];
        testArray2[0][0] = new TestArray2();
        testArray2[0][1] = new TestArray2();
        testArray2[1][0] = new TestArray2();
        testArray2[1][1] = new TestArray2();
        TestArray2[][] testArray3 = new TestArray2[2][2];
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                testArray3[i][j] = testArray2[i][j];
            }
        }
        System.out.println("=============拷贝完成=========");
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                System.out.print(testArray2[i][j].getVal()+" ");
            }
        }
        System.out.println();
        for (int i = 0; i < testArray3.length; i++) {
            for (int j = 0; j < testArray3[i].length; j++) {
                System.out.print(testArray3[i][j].getVal()+" ");
            }
        }
        System.out.println();

        testArray2[0][0].setVal(1000000);
        System.out.println("============修改完成=========");
        for (int i = 0; i < testArray2.length; i++) {
            for (int j = 0; j < testArray2[i].length; j++) {
                System.out.print(testArray2[i][j].getVal()+" ");
            }
        }
        System.out.println();
        for (int i = 0; i < testArray3.length; i++) {
            for (int j = 0; j < testArray3[i].length; j++) {
                System.out.print(testArray3[i][j].getVal()+" ");
            }
        }
        System.out.println();
    }

    public static void main2(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        int[][] brray = new int[2][3];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                brray[i][j] = array[i][j];
            }
        }
        System.out.println(Arrays.deepToString(array));
        System.out.println(Arrays.deepToString(brray));
        brray[0][0] = 1000;
        System.out.println(Arrays.deepToString(array));
        System.out.println(Arrays.deepToString(brray));
        /*int[][] brray = array;
        System.out.println(Arrays.deepToString(brray));*/
    }

    /*//重载函数
    public static int sum(int a,int b) {//sum(int,int )
        return a+b;
    }
    public static int sum(int a,int b,int c) {//sum(int,int ,int)
        return a+b+c;
    }
    public static int sum(int... array) {//函数签名
    }*/
    public static void main1(String[] args) {
        int[][] array = new int[2][3];
        int[][] array2 = {{1,2,3},{4,5,6},{7,8,9}};

        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.deepToString(array));

        int[][] array3 = new int[2][];
        array3[0] = new int[4];
        array3[1] = new int[5];

        System.out.println(Arrays.deepToString(array3));


    }
}

排序:从小到大排序 在array中 内部排序
冒泡:最好的时间复杂度O(n) 最坏的O(n^2)稳定
选择:时间复杂度O(n^2) 不稳定
直接插入排序:最坏的时间复杂度O(n^2) 最好的O(n)即有序的时候 稳定===>优化 shell排序 希尔排序
时间复杂度O(1) O(n) O(n^2) O(log2 N)
一维数组System.out.printin(Arrays.toString(array))
二维数组System.out.printin(Arrays.deepToString(array))
所有引用初始化都为null

public class TestDemo1 {

    public static void bubbleSort(int[] array) {
        int tmp = 0;
        boolean swap = false;
        for(int i = 0;i < array.length-1;i++) {
            for(int j = 0;j < array.length-1-i;j++) {
                if(array[j] > array[j+1]) {
                    tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    swap = true;
                }
            }
            if(!swap) {
                break;
            }
        }
    }

    public static void insertSort(int[] array) {
        int tmp = 0;
        int j = 0;
        for(int i = 1;i < array.length;i++) {
            tmp = array[i];
            for(j = i-1;j >= 0;j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void selectSort(int[] array) {
        int tmp = 0;
        for(int i = 0;i < array.length;i++) {
            for(int j = i+1;j < array.length;j++) {
                if(array[j] < array[i]) {
                    tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {12,1,3,5,2,8,78,7};
        selectSort(array);
        System.out.println(Arrays.toString(array));
    }
}

猜你喜欢

转载自blog.csdn.net/lmx9813/article/details/83387516