Java-day07 study notes

2021-01-06

day06 review

Insert picture description here

1. Method

1.1 Method overload

In the same class, the method name is the same, the parameter list is different, and it has nothing to do with the return value type

参数列表不同的三种情况:
1. 个数不同
2. 类型不同
3. 个数相同,类型也相同,但是前后位置不同
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        System.out.println(sum(3, 5));
        System.out.println(sum(3.0, 5.0));
        System.out.println(sum(3, 5, 8));
        System.out.println(sum(3, 5, 8, 10));
    }

    /*
        求两个整数的和

        两个明确:
            返回值类型: int
            参数列表: int a, int b
     */
    public static int sum(int a, int b){
    
    

        System.out.println("int...int...");
        return a + b;
    }

    /*
        求两个小数的和

        两个明确:
            返回值类型: double
            参数列表: double a, double b
     */
    public static double sum(double a, double b){
    
    

        System.out.println("double...double...");
        return a + b;
    }

    /*
        求三个整数的和

        两个明确:
            返回值类型: int
            参数列表: int a, int b, int c
     */
    public static int sum(int a, int b, int c){
    
    

        System.out.println("int...int...int...");
        return a + b + c;
    }

    /*
        求四个整数的和

        两个明确:
            返回值类型: String
            参数列表: int a, int b, int c, int d
     */
    public static String sum(int a, int b, int c, int d){
    
    

        System.out.println("int...int...int...int...");
        return "" + (a + b + c + d);
    }

}

Overloading of methods seen before:

print
println

1.2 Nested method calls

In one method, you can call other methods

public class Demo3 {
    
    

    public static void main(String[] args) {
    
    

        System.out.println("进入了main...");
        fun1();
        System.out.println("出去了main...");
    }

    private static void fun1() {
    
    

        System.out.println("进入了fun1...");
        fun2();
        System.out.println("出去了fun1...");
    }

    private static void fun2() {
    
    

        System.out.println("进入了fun2...");
        System.out.println("出去了fun2...");
    }
}
/*
结果:
    进入了main...
    进入了fun1...
    进入了fun2...
    出去了fun2...
    出去了fun1...
    出去了main...
  */

1.3 Method recursion

Method calls itself

Note: Recursion must have an exit (otherwise it will keep calling each other without stopping)

练习:
	求5的阶乘
/*
    求5的阶乘
    1! = 1
    2! = 2*1
    3! = 3*2*1
    4! = 4*3*2*1
    5! = 5*4*3*2*1
 */
public class Test {
    
    

    public static void main(String[] args) {
    
    

        //方式一:for循环
        /*
        int jc = 1;
        for(int i = 1; i <= 5; i++){

            jc *= i;
        }

        System.out.println("5的阶乘结果为:" + jc);
        */

        //方式二:递归
        System.out.println(jc(5));

    }

    /*
           两个明确:
                返回值类型:int
                参数列表:int x
     */
    public static int jc(int x){
    
      //x = 2

        if(x == 1){
    
    
            return 1;
        }

        return x * jc(x-1);
    }

}

Insert picture description here

Insert picture description here

Second, the array

An array is a container. The array container can store multiple elements of the same data type.

2.1 Array declaration

数据类型[] 数组名 = new 数据类型[数组的长度];
补充格式:数据类型 数组名[] = new 数据类型[数组的长度];

解释:
	数据类型:存放元素的类型,这个类型可以是基本数据类型,也可以是引用数据类型
	[]:表明了这是一个一维数组
	数组名:  就是一个变量名,符合命名规范即可(第一个单词全部小写,从第二个单词开始首字母大写)
	=:将创建好的数组的地址赋值给数组名
	new:是一个关键字,用来在堆内存上创建对象
	数据类型:同上
	数组的长度:数组中能存放多少元素(一旦定义好,就不能再次修改)

2.2 Initialization of the array

Array initialization is to assign initialization values ​​to the array

方式一:动态初始化(先声明一个数组,然后赋值)
		格式:
		数据类型[] 数组名 = new 数据类型[数组的长度];
		数组名[索引] = 数据;

方式二:静态初始化(在声明一个数组的同时就赋值)
		格式:
		数据类型[] 数组名 = new 数据类型[]{元素1, 元素2, 元素3, ...};
		简化格式:
		数据类型[] 数组名 = {元素1, 元素2, 元素3, ...};
		
补充:索引是什么?
	索引就是数组存放数据的位置,这个位置一般是从0开始计数的。索引又被称为下标、角标

2.3 Obtaining array data

数组名[索引]
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        //数组的声明:数据类型[] 数组名 = new 数据类型[数组的长度];
        int[] arr = new int[3];   //创建了一个能存放3个int类型的元素

        System.out.println(arr);  //[I@1b6d3586

        //方式一:动态初始化
        arr[0] = 11;
        arr[1] = 22;
        arr[2] = 33;

        //从数组中获取元素
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }

}
public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        //方式二:静态初始化
//        int[] arr = new int[]{11, 22, 33};

        //简化格式:
        int[] arr = {
    
    11, 22, 33};

        System.out.println(arr);
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);

    }
}

2.4 The printed content of the array name

int[] arr = new int[3];
System.out.println(arr);   //[I@1b6d3586

[I@1b6d3586

解释:
[:表明这是一个一维数组
I:表明这是一个int类型的数组
@:就是一个连接符,'@'
1b6d3586:实际上是真正地址的hashCode值的十六进制表示形式(相当于是地址)

2.5 Memory Division in JVM

栈:存放局部变量以及方法执行的地方
堆:存放的都是对象(也就是new出来的内容)
方法区:存放的有静态的变量、常量、方法、字节码文件
本地方法区:存放的由C或C++等组成的本地方法
程序计数器:控制程序的执行顺序的

补充:什么是局部变量?
	局部变量就是定义在方法上或者方法内部的变量

2.6 Memory map of array

① Memory map of an array

public class PicDemo {
    
    

    public static void main(String[] args) {
    
    

        int[] arr = new int[3];

        System.out.println(arr);      //[I@1b6d3586
        System.out.println(arr[0]);   //0
        System.out.println(arr[1]);   //0
        System.out.println(arr[2]);   //0
        System.out.println("----------------");

        arr[0] = 11;
        arr[1] = 22;
        arr[2] = 33;

        System.out.println(arr);     //[I@1b6d3586
        System.out.println(arr[0]);  //11
        System.out.println(arr[1]);  //22
        System.out.println(arr[2]);  //33

    }
}

Insert picture description here

② Memory map of two arrays

public class PicDemo2 {
    
    

    public static void main(String[] args) {
    
    

        //第一个数组
        int[] arr = new int[3];

        arr[0] = 11;
        arr[1] = 22;
        arr[2] = 33;

        System.out.println(arr);    //[I@1b6d3586
        System.out.println(arr[0]); //11
        System.out.println(arr[1]); //22
        System.out.println(arr[2]); //33
        System.out.println("----------------");

        //第二个数组
        int[] arr2 = new int[3];    //每new一次,都会在堆上开辟一个新的空间

        arr2[0] = 77;
        arr2[1] = 88;
        arr2[2] = 99;

        System.out.println(arr2);    //[I@4554617c
        System.out.println(arr2[0]); //77
        System.out.println(arr2[1]); //88
        System.out.println(arr2[2]); //99

    }
}

③ Two references point to the same array

public class PicDemo3 {
    
    

    public static void main(String[] args) {
    
    

        int[] arr = new int[3];

        System.out.println(arr);    //[I@1b6d3586
        System.out.println(arr[0]); //0
        System.out.println(arr[1]); //0
        System.out.println(arr[2]); //0
        System.out.println("----------------");

        arr[0] = 11;
        arr[1] = 22;
        arr[2] = 33;

        //将引用arr赋值给引用arr2
        int[] arr2 = arr;
        System.out.println(arr2[0]);  //11
        System.out.println(arr2[1]);  //22
        System.out.println(arr2[2]);  //33
        System.out.println("----------------");

        arr2[1] = 99;

        System.out.println(arr[0]);  //11
        System.out.println(arr[1]);  //99
        System.out.println(arr[2]);  //33
        System.out.println("----------------");

        System.out.println(arr2[0]); //11
        System.out.println(arr2[1]); //99
        System.out.println(arr2[2]); //33

    }
}

Insert picture description here

2.7 Expansion of array

For example: an array int[] arr = new int[3]; can only store three elements, now we just want this array to store four elements, what should we do?

import java.util.Arrays;

/**
 * 数组的扩容
 */
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        int[] arr = {
    
    11, 22, 33};

        //方式一:新建一个数组,然后将新数组的地址赋值给原数组
        /*
        //1. 新建一个数组,比原数组的长度大1
        int[] arr_xin = new int[4];
        //2. 遍历原数组将原数组中的内容,赋值给新数组
        for(int i = 0; i < 3; i++){
            arr_xin[i] = arr[i];
        }
        //3. 将新数组的地址赋值给原数组
        arr = arr_xin;
        */

        /*
            方式二:通过Arrays中提供的copyOf方法

            copyOf(int[] arr, int newLength)
                参数arr:是要被扩容的原数组
                参数newLength:是要扩容后的长度
         */
         
        /*
        //1. 通过Arrays的copyOf方法产生一个指定长度的新数组
        int[] arr_xin = Arrays.copyOf(arr, 4);
        //2. 将新数组的地址赋值给原数组
        arr = arr_xin;
        */

        /*
            方式三:通过System类中提供的arraycopy方法

            arraycopy(int[] src, int srcPos, int[] dest, int destPos, int length)
                参数src:是原数组
                参数srcPos:是从原数组哪个索引开始复制(starting position in the source array.)
                参数dest:是新数组
                参数destPos:从新数组哪个索引开始(starting position in the destination data.)
                参数length:是复制多个长度
         */
         
        int[] arr_xin = new int[4];
        System.arraycopy(arr, 0, arr_xin, 0, 3);

        arr = arr_xin;

        //访问
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
        System.out.println(arr[3]);
    }
}

Guess you like

Origin blog.csdn.net/ChangeNone/article/details/112282422