Java novice learning 2021-1-25 record daily learning content (if there is any infringement, please contact to delete!!!)

2021-1-25

1. Basic use of Api (Api introduction)

Insert picture description here

2. Use Math tool class (Math common method)

Static method, can be called directly
Insert picture description here

3. System use (exit/currentTimeMillis)

Insert picture description here
system.exit When the code is executed to this method, it means that the virtual machine has stopped, and the status can usually be filled with 0
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

4. Object class (to String/equals and other methods)

Insert picture description here
Insert picture description here
Insert picture description here

5.Objects类(to String/isNull/nonNull)

Insert picture description here

6.BigDecimal class

BigDecimal construction: (understand)
Insert picture description here
Insert picture description here
BigDecimal common methods
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

7. Basic types of packaging (mainly master Integer)

Insert picture description here
Insert picture description here

8. Automatic boxing and automatic unboxing

Insert picture description here
Insert picture description here

9. Type conversion (conversion between int and String)

Insert picture description here
Insert picture description here

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/26/19:55
 */
public class StudentDemo {
    
    
    public static void main(String[] args) {
    
    
        Student student=new Student();
        student.setName("张三");
        student.setId(""+1);
        student.setId(String.valueOf(1));
        System.out.println(student);
    }
}

Case study
Insert picture description here

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/26/20:09
 */
public class ArrayToInt {
    
    
    public static void main(String[] args) {
    
    
        String arr="99 23 34 12 15";
        String[] s = arr.split(" ");

        int[] numArr=new int[s.length];

        for (int i = 0; i < s.length; i++) {
    
    
            int parseInt = Integer.parseInt(s[i]);
            numArr[i]=parseInt;
        }
        for (int i = 0; i < numArr.length; i++) {
    
    
            System.out.println(numArr[i]);
        }
    }
}

10. Advanced operations of arrays (dichotomy)

Insert picture description here

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/27/11:51
 */
public class ArrayMid {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int number = 3;
        int index = getIndex(arr, number);
        System.out.println(index);
    }

    private static int getIndex(int[] arr, int number) {
    
    
        //1.定义查找范围
        int min = 0;
        int max = arr.length - 1;
        //循环查找min <= max
        while (min <= max) {
    
    
            //求出中间元素
            int mid = (min + max) >> 1;
            //判断number与mid的大小关系
            if (arr[mid] > number) {
    
    
                //要查找的元素在左边
                max = mid - 1;
            } else if (arr[mid] < number) {
    
    
                //要查找的元素在右边
                min = mid + 1;
            }
            if (arr[mid] == number) {
    
    
                return mid;
            }
        }
        //当min>max时,说明元素不在范围内
        return -1;
    }
}

11. Bubble sort

Insert picture description here

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/27/15:31
 */
public class BubbleSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    3, 5, 2, 1, 4};

        //i < arr.length-1 减一是为了防止索引越界
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            if (arr[i] > arr[i + 1]) {
    
    
                //数据交换
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        printArr(arr);

        for (int i = 0; i < arr.length - 1 - 1; i++) {
    
    
            if (arr[i] > arr[i + 1]) {
    
    
                //数据交换
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        printArr(arr);

        for (int i = 0; i < arr.length - 1 - 1 - 1; i++) {
    
    
            if (arr[i] > arr[i + 1]) {
    
    
                //数据交换
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        printArr(arr);

        for (int i = 0; i < arr.length - 1 - 1 - 1 - 1; i++) {
    
    
            if (arr[i] > arr[i + 1]) {
    
    
                //数据交换
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        printArr(arr);
    }

    private static void printArr(int[] arr) {
    
    
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

Code optimization, common method for extraction

package com.wc.show;

/**
 * @author wc
 * @Date: 2021/01/27/15:31
 */
public class BubbleSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    3, 5, 2, 1, 4};
        changeArr(arr);

    }

    private static void changeArr(int[] arr) {
    
    
        //第一层for是循环次数
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            //第二层是交换次数
            //i < arr.length-1 减一是为了防止索引越界
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
                if (arr[j] > arr[j + 1]) {
    
    
                    //数据交换
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        printArr(arr);
    }
    private static void printArr(int[] arr) {
    
    
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

Guess you like

Origin blog.csdn.net/weixin_49221590/article/details/113123373