java基础

一 .三种排序:
(1)冒泡排序:

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 + 1];
                    array[j+1] = array[j];
                    array[j] = tmp;
                    swap = true;
                }
            }
            if (!swap) {
                break;
            }
        }
    }
   public static void main(String[] args) {
    int[] arry = new int[]{1, 3, 5, 6, 8, 4};
     bubbleSort(arry);
      System.out.printf(Arrays.toString(arry));
    }
}

最好情况下的时间复杂度:O(n),即就是给定的数组是有序的。
最坏情况下的时间复杂度:O(n^2).
稳定性:稳定。
(2)直接插入排序:

public class TestDemo1 {
public static void insertsort(int[] array) {
    int tmp = 0;
    int j = 0;
    for (int i = 1; i < array.length; i++) {
        array[i] = tmp;
        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 main(String[] args) {
    int[] array = new int[]{1, 3, 5, 6, 8, 4};
   insertSort(array);
      System.out.printf(Arrays.toString(array));
    }
}

最好情况下的时间复杂度:O(n),即就是给定的数组是有序的。
最坏情况下的时间复杂度:O(n^2).
稳定性:稳定。
(3)选择排序

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

        }
    }
}
public static void main(String[] args) {
    int[] array = new int[]{1, 3, 5, 6, 8, 4};
       selectSort(array);
       System.out.printf(Arrays.toString(array));
    }
}

最好/最坏的情况下它的时间复杂度还是O(n^2).
稳定性:不稳定,具有跳跃性。
二.重载函数:

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;
    }

二维数组的定义:

//规则的二维数组
   int[][] array = new int[2][3];
        int[][] array2 = {{1,2,3},{4,5,6},{7,8,9}};
//不规则的二维数组:
  int[][] array3 = new int[2][];
        array3[0] = new int[4];
        array3[1] = new int[5];

二维数组的拷贝:
for循环:

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

    public static void main(String[] args) {
        Array2 [][] Arrays2 = new Array2 [2][2];
        Arrays2[0][0] = new Array2 ();
        Arrays2[0][1] = new Array2 ();
        Arrays2[1][0] = new Array2 ();
        Arrays2[1][1] = new Array2 ();
        Array2 [][] testArray3 = new Array2 [2][2];
        for (int i = 0; i < Arrays2.length; i++) {
            for (int j = 0; j < Arrays2[i].length; j++) {
                testArray3[i][j] = Arrays2[i][j];
            }
        }
        System.out.println("=============拷贝完成=========");
        for (int i = 0; i < Arrays2.length; i++) {
            for (int j = 0; j < Arrays2[i].length; j++) {
                System.out.print(Arrays2[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();

        Arrays2[0][0].setVal(10);
        System.out.println("============修改完成=========");
        for (int i = 0; i < Arrays2.length; i++) {
            for (int j = 0; j < Arrays2[i].length; j++) {
                System.out.print(Arrays2[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();
    }

}

.clone:

class Array2 {
    private int val;

    public void setVal(int val) {
        this.val = val;
    }

    public int getVal() {
        return this.val;
    }

    public static void main(String[] args) {
        Array2[][] Arrays2 = new Array2[2][2];
        Arrays2[0][0] = new Array2();
        Arrays2[0][1] = new Array2();
        Arrays2[1][0] = new Array2();
        Arrays2[1][1] = new Array2();
        Array2[][] testArray3 = new Array2[2][2];
        //clone
        for (int i = 0; i < Arrays2.length; i++) {
            testArray3[i] = Arrays2[i].clone();
        }
        System.out.println("=============拷贝完成=========");
        for (int i = 0; i < Arrays2.length; i++) {
            for (int j = 0; j < Arrays2[i].length; j++) {
                System.out.print(Arrays2[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();

        Arrays2[0][0].setVal(10);
        System.out.println("============修改完成=========");
        for (int i = 0; i < Arrays2.length; i++) {
            for (int j = 0; j < Arrays2[i].length; j++) {
                System.out.print(Arrays2[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();

    }
}

System.arraycopy:

class Array2 {
    private int val;

    public void setVal(int val) {
        this.val = val;
    }

    public int getVal() {
        return this.val;
    }

    public static void main(String[] args) {
        Array2[][] Arrasy2 = new Array2[2][2];
        Arrasy2[0][0] = new Array2();
        Arrasy2[0][1] = new Array2();
        Arrasy2[1][0] = new Array2();
        Arrasy2[1][1] = new Array2();
        Array2[][] testArray3 = new Array2[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(Arrasy2);
        testArray3 = Arrays.copyOf(Arrasy2, Arrasy2.length);
        System.out.println(testArray3);
        System.out.println(Arrasy2);


        System.out.println("=============拷贝完成=========");
        for (int i = 0; i < Arrasy2.length; i++) {
            for (int j = 0; j < Arrasy2[i].length; j++) {
                System.out.print(Arrasy2[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();

        Arrasy2[0][0].setVal(10);
        System.out.println("============修改完成=========");
        for (int i = 0; i < Arrasy2.length; i++) {
            for (int j = 0; j < Arrasy2[i].length; j++) {
                System.out.print(Arrasy2[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();

    }
}

Arrays.copyOf

class Array2 {
    private int val;

    public void setVal(int val) {
        this.val = val;
    }

    public int getVal() {
        return this.val;
    }
    public static void main(String[] args) {
        Array2[][] array1 = new Array2[2][2];
        array1[0][0] = new Array2();
        array1[0][1] = new Array2();
        array1[1][0] = new Array2();
        array1[1][1] = new Array2();
        Array2[][] array2 = new Array2[2][2];
        //array2 = Arrays.copyOf(array1, array1.length);
        //或者将循环中的Arrays.copyOf方法删掉,用上一行的方法拷贝
        for (int i = 0; i < array1.length; i++) {
            array2[i] = Arrays.copyOf(array1[i], array1[i].length);
            for (int j = 0; j < array1[i].length; j++) {
                // array2[j]=array1[j].clone();
                System.out.print(array2[i][j].getVal() + " ");
            }
            System.out.println();
        }
        array2[0][0].setVal(100);
        System.out.println("================修改后===============");
        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j].getVal() + " ");
            }
            System.out.println();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值