B

252 阅读21分钟

第一章.Random随机数

 1.概述:java提前定义好的类
 2.作用:在指定的范围内随机一个数
 3.怎么用:
   a.导包:import java.util.Random
   b.创建对象:Random 名字 = new Random()
   c.调用方法,实现随机数:
     nextInt()->在int范围内随机一个数
     nextDouble()->在0.01.0之间随机
     nextInt(int bound)-> 从0到(bound-1)之间随机
 public class Test01 {
     public static void main(String[] args) {
         Random rd = new Random();
         int data = rd.nextInt();
         System.out.println(data);
     }
 }
 public class Test02 {
     public static void main(String[] args) {
         Random random = new Random();
         int data = random.nextInt(100);
         System.out.println(data);
     }
 }
 ​
 需求:
   1.1-10之间随机:nextInt(10)+1  -> (0-9)+1
   2.1-100之间随机:nextInt(100)+1-> (0-99)+1
   3.100-999之间随机:nextInt(900)+100->(0-899)+100
 public class Test03 {
     public static void main(String[] args) {
         Random random = new Random();
         int data = random.nextInt(900)+100;
         System.out.println(data);
     }
 }

第二章.数组的介绍以及定义

 1.概述:容器,数组本身属于引用数据类型
 2.特点:
   a.数组的长度固定->定长
   b.数组既能存储基本数据类型的数据也能存储引用数据类型的数据
 3.定义:
   a.动态初始化
     数据类型[] 数组名 = new 数据类型[数组的长度]->一般推荐
     数据类型 数组名[] = new 数据类型[数组的长度]->一般不推荐
       
     各部分解释:
       数据类型:表明了数组能存什么类型的数据(元素)
       []:代表的是数组
       数组名:给数组起名字
       new:创建数组
       数据类型:要和等号左边的数据类型一致
       [数组长度]:给数组指定一个长度,表明数组最多能存多少个数据
   b.静态初始化
     数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3....}->不推荐
     数据类型 数组名[] = new 数据类型[]{元素1,元素2,元素3....}->不推荐
     
     简化:数据类型[] 数组名 = {元素1,元素2,元素3....}-> 推荐
         
 4.静态初始化和动态初始化的使用时机
   a.动态初始化:当只知道类型和长度,但是不确定具体后面存什么
   b.静态初始化:定义的时候就已经确定存啥了
 public class Test01 {
     public static void main(String[] args) {
         //动态初始化
         int[] arr = new int[3];
 ​
         String[] arr2 = new String[3];
 ​
         //静态初始化
         int[] arr3 = {1,2,3,4,5};
         
         String[] arr4 = {"纪晓岚","和珅","乾隆","嘉庆"};
     }
 }

第三章.操作数组

1.获取数组的长度

 1.格式:
   数组名.length
 2.length是数组的一个属性
 public class Test02 {
     public static void main(String[] args) {
         int[] arr = {1,2,3,45,4,4,6,6,5,5,6,6,5,5,6,7,7};
         int len = arr.length;
         System.out.println(len);
     }
 }

2.索引

 1.概述:
   每一个元素在数组中保存的位置(编号,下标)
 2.特点:
   a.唯一,不可重复
   b.从0开始,最大索引是数组的长度-1(arr.length-1)
 3.注意:
   以后存数据,取数据都要通过索引来完成

3.存储元素

1.格式:
  数组名[索引值] = 值
public class Test03 {
    public static void main(String[] args) {
        //创建数组
        int[] arr = new int[3];
        arr[0] = 100;
        arr[1] = 200;
        arr[2] = 300;

        System.out.println("========================");
        
        int[] arr1 = new int[3];
        //创建Random对象
        Random rd = new Random();
       /* int element1 = rd.nextInt(10);
        int element2 = rd.nextInt(10);
        int element3 = rd.nextInt(10);
        //往数组中存
        arr1[0] = element1;
        arr1[1] = element2;
        arr1[2] = element3;*/
       
        /*arr1[0] = rd.nextInt(10);
        arr1[1] = rd.nextInt(10);
        arr1[2] = rd.nextInt(10);*/

        for (int i = 0; i < 3; i++) {
            arr1[i] = rd.nextInt(10);
        }

        System.out.println("==============================");
        
        int[] arr2 = new int[3];
        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < 3; i++) {
            arr2[i] = sc.nextInt();
        }
       
    }
}

4.获取元素

1.格式:
  数据类型 变量名 = 数组名[索引值]
2.注意:
  a.直接输出数组名,输出的是地址值
  b.数组中的元素都是有默认值的
数组中元素的默认值:
  整数:  0
  小数:  0.0
  字符:  '\u0000'
  布尔:  false
  引用:  null
 public class Test04 {
     public static void main(String[] args) {
         int[] arr1 = new int[3];
         System.out.println(arr1);//[I@1540e19d
         //数据类型 变量名 = 数组名[索引值]
         int element1 = arr1[0];
         int element2 = arr1[1];
         int element3 = arr1[2];
 ​
         System.out.println(element1);//0
         System.out.println(element2);//0
         System.out.println(element3);//0
 ​
         System.out.println("========================");
 ​
         String[] arr2 = new String[3];
         System.out.println(arr2);//[I@1540e19d
         //数据类型 变量名 = 数组名[索引值]
         String element4 = arr2[0];
         String element5 = arr2[1];
         String element6 = arr2[2];
 ​
         System.out.println(element4);//null
         System.out.println(element5);//null
         System.out.println(element6);//null
 ​
     }
 }
 ​
 public class Test05 {
     public static void main(String[] args) {
         String[] arr = new String[3];
         //存数据
         arr[0] = "张无忌";
         arr[1] = "尹志平";
         arr[2] = "赵敏";
         //取数据
         System.out.println(arr[0]);
         System.out.println(arr[1]);
         System.out.println(arr[2]);
     }
 }
 arr[0] = arr1[1]
 数组名[索引值]在等号右边,代表取值,在左边,代表存值
     
 先将arr1数组1索引上的元素获取出来存放到arr数组的0索引上
 public class Test06 {
     public static void main(String[] args) {
         String[] arr = {"迪丽热巴","古力娜扎","马尔扎哈"};
         String[] arr1 = new String[3];
         arr1[0] = arr[0];
         arr1[1] = arr[1];
         arr1[2] = arr[2];
 ​
         System.out.println(arr1[0]);
         System.out.println(arr1[1]);
         System.out.println(arr1[2]);
     }
 }
需求:定义长度为3的数组,元素通过键盘录入确定,存到对应的索引位置上
public class Test07 {
    public static void main(String[] args) {
        //创建Scanner对象
        Scanner sc = new Scanner(System.in);
        //定义长度为3的数组
        int[] arr = new int[3];

        for (int i = 0; i < 3; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }
}
需求:定义长度为3的数组,三个元素通过随机数确定
public class Test08 {
    public static void main(String[] args) {
        //创建Random对象
        Random rd = new Random();
        //定义长度为3的数组
        int[] arr = new int[3];

        for (int i = 0; i < 3; i++) {
            arr[i] = rd.nextInt(100);
        }

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

5.遍历数组

1.概述:将元素从数组中挨个获取出来
public class Test09 {
    public static void main(String[] args) {
        String[] arr = {"张无忌", "周芷若", "赵敏", "小昭", "朱九真"};
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
        System.out.println(arr[3]);
        System.out.println(arr[4]);

        System.out.println("========================");

        for (int i = 0; i < 5; i++) {
            System.out.println(arr[i]);
        }

        System.out.println("========================");

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

快捷键:数组名.fori

第四章.操作数组时两个常见的问题

1.数组索引越界异常->ArrayIndexOutOfBoundsException

1.出现的原因:
  操作的索引,超出了数组的索引范围
public class Test10Problem {
    public static void main(String[] args) {
        int[] arr = new int[3];
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
        //System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: 3
        System.out.println(arr[-3]);//ArrayIndexOutOfBoundsException: 3
    }
}

2.空指针异常_NullPointerException

1.出现的原因:
  当数组为null,然后再操作数组
public class Test11Problem {
    public static void main(String[] args) {
        int[] arr = new int[3];
        arr = null;
        //System.out.println(arr.length);NullPointerException

    }
}

image-20220217154432869

知道这两个问题出现的原因

第五章.练习

1练习

 第一题:需求:求出数组中的元素最大值
 步骤:
   1.定义一个变量max,用于接收两个数比较之后较大的那个值
   2.循环比较,谁大,就将大的赋值给max
   3.继续和后面的元素进行比较,谁大,将大的元素,赋值给max
   4.输出max

image-20220217155507960

 public class Test01 {
     public static void main(String[] args) {
         int[] arr = {4,3,5,2,1};
         //1.定义一个变量max,用于接收两个数比较之后较大的那个值
         int max = arr[0];
         for (int i = 1; i < arr.length; i++) {
         //2.循环比较,谁大,就将大的赋值给max
         //3.继续和后面的元素进行比较,谁大,将大的元素,赋值给max
             if (max<arr[i]){
                 max = arr[i];
             }
         }
         //4.输出max
         System.out.println(max);
     }
 }

2练习

 随机产生10[0,100]之间整数,统计既是3又是5,但不是7的倍数的个数
     
 步骤:
   1.创建Random对象
   2.定义长度为10的数组
   3.调用nextInt(101),随机10个,存储到数组中
   4.定义count,用来统计个数
   5.遍历数组,判断,符合条件,count++
   6.输出count
 public class Test02 {
     public static void main(String[] args) {
         //1.创建Random对象
         Random rd = new Random();
         //2.定义长度为10的数组
         int[] arr = new int[10];
         //3.调用nextInt(101),随机10个,存储到数组中
         for (int i = 0; i < arr.length; i++) {
             arr[i] = rd.nextInt(101);
         }
         //4.定义count,用来统计个数
         int count = 0;
         //5.遍历数组,判断,符合条件,count++->  3又是5,但不是7的倍数的个数
         for (int i = 0; i < arr.length; i++) {
             if (arr[i]%3==0 && arr[i]%5==0 && arr[i]%7!=0){
                 count++;
             }
         }
         //6.输出count
         System.out.println(count);
     }
 }

3练习

 用一个数组存储本组学员的姓名,从键盘输入,并遍历显示
 public class Test03 {
     public static void main(String[] args) {
         String[] arr = new String[3];
         Scanner sc = new Scanner(System.in);
         for (int i = 0; i < arr.length; i++) {
             System.out.println("请您输入第"+(i+1)+"个学生姓名:");
             arr[i] = sc.next();
         }
         //遍历
         for (int i = 0; i < arr.length; i++) {
             System.out.println(arr[i]);
         }
     }
 }

4练习

 需求:
   1.定义一个数组 int[] arr = {1,2,3,4}
   2.遍历数组,输出元素按照[1,2,3,4]
 //[1,2,3,4]
 public class Test04 {
     public static void main(String[] args) {
         int[] arr = {1, 2, 3, 4};
         System.out.print("[");
         for (int i = 0; i < arr.length; i++) {
             if (i == arr.length - 1) {
                 System.out.print(arr[i] + "]");
             } else {
                 System.out.print(arr[i] + ", ");
 ​
             }
         }
     }
 }

5练习

 键盘录入一个整数,找出整数在数组中存储的索引位置
 步骤:
   1.创建Scanner对象
   2.定义数组,存储数据
   3.调用nextInt方法,输入要查找的元素
   4.遍历数组,判断如果数组中的元素和输入的数据相等,直接输出对应的索引值
 public class Test05 {
     public static void main(String[] args) {
         //1.创建Scanner对象
         Scanner sc = new Scanner(System.in);
         //2.定义数组,存储数据
         int[] arr = {11,22,33,44,55,66};
         //3.调用nextInt方法,输入要查找的元素
         int data = sc.nextInt();
         //定义一个变量,记录数组中到底有没有要查找的数据
         int index = 0;
         //4.遍历数组,判断如果数组中的元素和输入的数据相等,直接输出对应的索引值
         for (int i = 0; i < arr.length; i++) {
             if (arr[i]==data){
                 System.out.println(i);
                 //进了if,证明肯定是找到了
                 index++;
             }
         }
         //判断,如果index的值还等于0,证明没劲if,没进if证明数组中没有要找的数据
         if (index==0){
             System.out.println("对不起,没找到");
         }
     }
 }
 ​

第六章.内存(了解)

 1.栈(Stack):
   专门运行方法的,所有的方法(除本地方法外)的运行都会在栈内存中运行
 2.堆(Heap)
   每new一次,堆内存都会为其开辟一个空间,并为此空间分配一个地址值
   堆内存中的数据都是有默认值的:
   整数:0
   小数:0.0
   字符:'\u0000'
   布尔:false
   引用:null
 3.方法区(Method Area)
   好比是一个代码运行之前的预备区
   记录着类的信息,以及方法的信息
 4.本地方法栈(Native Method Stack)
   专门运行本地方法(在方法中带native关键字)
   本地方法:看不到方法体->C语言编写
   本地方法可以理解为对java中方法的一个扩充
 5.寄存器(pc Register):和系统有关

image-20220218092127110

1.一个数组在内存中如何存储

 public class Test01 {
     public static void main(String[] args) {
         int[] arr = new int[3];
         System.out.println(arr);//地址值
         arr[1] = 100;
         System.out.println(arr[1]);//100
         System.out.println(arr[2]);//0
     }
 }

image-20220218093805957

2.两个数组内存图

public class Test02 {
    public static void main(String[] args) {
        int[] arr1 = new int[3];
        System.out.println(arr1);//地址值
        arr1[1] = 100;

        int[] arr2 = new int[3];
        System.out.println(arr2);//地址值
        arr2[1] = 200;

        System.out.println(arr1[1]);//100
        System.out.println(arr2[1]);//200
    }
}


创建了两个数组,在堆内存中开辟了两个不同的空间,操作一个空间中的元素不会影响到另外一个空间的元素

image-20220218095444184

3.两个数组指向同一个空间

public class Test03 {
    public static void main(String[] args) {
        int[] arrA = new int[3];
        System.out.println(arrA);//地址值
        arrA[1] = 100;

        int[] arrB = arrA;
        System.out.println(arrB);//地址值
        arrB[1] = 200;

        System.out.println(arrA[1]);//200
        System.out.println(arrB[1]);//200
    }
}

arrB是arrA直接赋值得来的,所以arrB的地址值和arrA的地址值一样,所以改变一个数组的数据会影响到另外一个数组

image-20220218101722439

#第二章.冒泡排序

数组的排序,是将数组中的元素按照大小进行排序,默认都是以升序的形式进行排序,数组排序的方法很多,我们讲解的是数组的冒泡排序。

排序,都要进行数组 元素大小的比较,再进行位置的交换。冒泡排序法是采用数组中相邻元素进行比较换位。

2.1 冒泡排序图解

image-20220218103823385

2.2 代码实现

public class Test04_MaoPao {
    public static void main(String[] args) {
        int[] arr = {7,6,5,4,3};
       /* for (int i = 0; i < arr.length-1-0; i++) {
            if (arr[i]>arr[i+1]){
                int temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
            }
        }
        //直接查看数组中的数据
        System.out.println(Arrays.toString(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;
            }
        }
        //直接查看数组中的数据
        System.out.println(Arrays.toString(arr));

        for (int i = 0; i < arr.length-1-2; i++) {
            if (arr[i]>arr[i+1]){
                int temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
            }
        }
        //直接查看数组中的数据
        System.out.println(Arrays.toString(arr));

        for (int i = 0; i < arr.length-1-3; i++) {
            if (arr[i]>arr[i+1]){
                int temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
            }
        }
        //直接查看数组中的数据
        System.out.println(Arrays.toString(arr));*/

        for (int j = 0; j < arr.length-1; j++) {
            for (int i = 0; i < arr.length-1-j; i++) {
                if (arr[i]>arr[i+1]){
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

第七章.二维数组

1.二维数组的定义格式

 1.概述:二维数组中有多个一维数组
 2.定义:
   a.动态初始化:
     数据类型[][] 数组名 = new 数据类型[m][n]->推荐
     数据类型 数组名[][] = new 数据类型[m][n]->一般般
     数据类型[] 数组名[] = new 数据类型[m][n]->不推荐
         
     m:代表的是二维数组的长度->二维数组中有多少个一维数组
     n:代表的是每一个一维数组的长度
         
     数据类型[][] 数组名 = new 数据类型[m][]->代表的是二维数组中的每一个一维数组没有被初始化
         
   b.静态初始化:
     数据类型[][] 数组名 = new 数据类型[][]{{元素1,元素2...},{元素1,元素2...}...}
     简化:数据类型[][] 数组名 = {{元素1,元素2...},{元素1,元素2...}...}

image-20220218113208020

public class Test01 {
    public static void main(String[] args) {
        //动态初始化
        int[][] arr1 = new int[3][3];
        int arr2[][] = new int[3][3];
        int[] arr3[] = new int[3][3];
        
        //静态初始化
        String[][] arr4 = {{"柳岩","三上","松下"},{"岳不群","东方不败","林平之"},{"孙悟空","二郎神"}};
    }
}

2.获取二维数组长度

1.格式:
  数组名.length
public class Test02 {
    public static void main(String[] args) {
        //静态初始化
        String[][] arr1 = {{"柳岩","三上","松下"},{"岳不群","东方不败","林平之"},{"孙悟空","二郎神"}};
        System.out.println(arr1.length);
        //遍历二维数组,将每一个一维数组获取出来->获取每一个一维数组长度
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i].length);
        }
    }
}

3.获取二维数组中的元素

1.格式:
  数组名[i][j]
2.i:代表的是一维数组在二维数组中的索引位置
  j:代表的元素在一维数组中的索引位置
public class Test03 {
    public static void main(String[] args) {
        //静态初始化
        String[][] arr1 = {{"柳岩","三上","松下"},{"岳不群","东方不败","林平之"},{"孙悟空","二郎神","沉香"}};
        System.out.println(arr1[0][1]);
        System.out.println(arr1[1][2]);
    }
}

image-20220218114654722

##3.4二维数组中存储元素

 1.格式:
  数组名[i][j] = 元素
2.i:代表的是一维数组在二维数组中的索引位置
  j:代表的元素在一维数组中的索引位置
public class Test04 {
    public static void main(String[] args) {
        int[][] arr = new int[3][3];
        arr[0][0] = 100;
        arr[0][1] = 200;
        arr[0][2] = 300;

        arr[1][0] = 1000;
        arr[1][1] = 2000;
        arr[1][2] = 3000;

        arr[2][0] = 10000;
        arr[2][1] = 20000;
        arr[2][2] = 30000;

        
    }
}

4.二维数组的遍历

1.先把每一个一维数组从二维数组遍历出来,然后再将元素从每一个一维数组中遍历出来
public class Test04 {
    public static void main(String[] args) {
        int[][] arr = new int[3][3];
        arr[0][0] = 100;
        arr[0][1] = 200;
        arr[0][2] = 300;

        arr[1][0] = 1000;
        arr[1][1] = 2000;
        arr[1][2] = 3000;

        arr[2][0] = 10000;
        arr[2][1] = 20000;
        arr[2][2] = 30000;

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println(arr[i][j]);
            }
        }

    }
}

5.二维数组内存图

public class Test04 {
    public static void main(String[] args) {
        //代表只规定了二维数组的长度,但是没有初始化里面的一维数组,所以默认为null
        int[][] arr1 = new int[3][];
        //相当于在二维数组中的1索引位置上创建了一个存有1 2 3的一维数组
        arr1[1] = new int[]{1,2,3};
        //相当于在二维数组中的2索引位置上创建了一个长度为3的一维数组,没有元素
        arr1[2] = new int[3];
        //相当于在二维数组中的2索引位置上的一维数组的1索引位置上存放了100
        arr1[2][1] = 100;
    }
}

image-20220218141420718

第八章.方法的使用

1.方法介绍以及简单方法定义(无参无返回值)

1.概述:拥有功能性代码的代码块,一个功能就是一个方法
2.好处:好维护,好调用
3.定义:通用
  修饰符 返回值类型 方法名(参数列表){
     方法体
     return 结果
  }

  无参无返回值的方法
  有参无返回值的方法
  无参有返回值的方法
  有参有返回值的方法
1.定义格式:public static void 方法名(){
              方法体
          } 
2.注意:void 代表无返回值;[return 结果]代表的是有返回值
    
3.调用:
  在其他方法中:方法名();
public class Test01 {
    public static void main(String[] args) {
         farmer();
         cooker();
         cooker();
         eatPerson();

    }

    //农民伯伯
    public static void farmer(){

        System.out.println("播种");
        System.out.println("浇水");
        System.out.println("施肥");
        System.out.println("除草");
        System.out.println("驱虫");
        System.out.println("收割");
    }

    //厨师
    public static void cooker(){
        System.out.println("洗菜");
        System.out.println("切菜");
        System.out.println("炒菜");
        System.out.println("尝一尝");
        System.out.println("装盘");

    }
    //干饭人
    public static void eatPerson(){
        System.out.println("洗手");
        System.out.println("干饭");
    }
}
定义一个方法,实现10和20相加,将结果输出
public class Test02 {
    public static void main(String[] args) {
        sum();
    }

    //定义方法,实现两个整数相加
    public static void sum(){
        int a = 10;
        int b = 20;
        int he = a+b;
        System.out.println(he);
    }
}

2.无参数无返回值的方法执行流程

image-20220218144457568

3.方法定义各部分解释

 修饰符 返回值类型 方法名(参数列表){
    方法体
    return 结果
 }
 ​
 1.修饰符:public static
 2.返回值类型:该方法返回的结果的数据类型,要和方法体中return后面的那个数据类型一致 
             如果方法没有返回值,请用void代表
 3.方法名:遵循小驼峰式,见名知意
 4.参数:进入到方法内部参与运行的数据
       数据类型 变量名,数据类型 变量名
 5.方法体:实现该方法的具体代码,在方法体中要操作传进来的参数
 6.return 结果:代表返回值,操作参数之后得出的最终结果

4.有参数无返回值的方法定义和执行流程

 1.格式:
   public static void 方法名(参数){
     方法体
   }
 2.调用:直接调用
   方法名(具体的值)
 需求:定义一个方法,实现两个整数相加
 public class Test03 {
     public static void main(String[] args) {
         sum(10,20);
     }
 ​
     public static void sum(int a,int b){
         System.out.println(a+b);
     }
 }

image-20220218152544880

5.无参数有返回值定义以及执行流程

1.格式:
  public static 返回值类型 方法名(){
     方法体;
     return 结果
  }
2.调用:方法返回值返回给了谁,哪里调用了该方法,该方法的返回值就给到哪里
  赋值调用:
    数据类型 变量名 = 方法名()
  打印调用
    sout(方法名())
1.需求:两个整数相加将结果返回
public class Test04 {
    public static void main(String[] args) {
        /*赋值调用:
          数据类型 变量名 = 方法名()*/
        int result = sum();
        System.out.println(result);
        /*打印调用
          sout(方法名())*/

        System.out.println(sum());
    }

    public static int sum(){
        int i = 10;
        int j = 20;
        int sum = i+j;
        return sum;
    }
}

image-20220218154051469

6.有参数有返回值定义以及执行流程

1.格式:
  public static 返回值类型 方法名(参数){
     方法体
     return 结果
  }

2.调用
   赋值调用:
    数据类型 变量名 = 方法名(具体的值)
  打印调用
    sout(方法名(具体的值))
1.需求:两个整数相加将结果返回
public class Test05 {
    public static void main(String[] args) {
        int result = sum(10,20);
        System.out.println(result);
    }

    public static int sum(int a,int b){
        int sum = a+b;
        return sum;
    }
}

image-20220218155324037

7.形式参数和实际参数区别

形式参数:
  在定义方法的时候,定义的没有具体值的参数
实际参数:
  在调用的时候为参数赋的值

8.参数和返回值使用的时机

1.参数:
  a.如果定义方法的时候,不知道要操作什么数据,我们就可以定义参数,等着调用的时候确定具体的值
  b.如果想将一个方法中的数据,传递到另外一个方法中,那么另外一个方法就需要定义参数,等着接收要传递过来的数据
      
2.返回值:
  如果调用者想要被调用方法的结果,那么被调用方法就需要将结果返回

image-20220218163127891

9.变量作为实参使用

需求:定义一个方法,比较两个整数的大小,如果第一个参数比第二个参数大,返回true,否则返回false;

参数:要
方法名:要
返回值:要
public class Test06 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        boolean compare = compare(a,b);
        System.out.println(compare);
    }

    public static boolean compare(int a,int b){
        if (a>b){
            return true;
        }else{
            return false;
        }
    }
}

第九章.方法注意事项终极版

1.方法不调用不执行
2.方法之前不能互相嵌套,方法和方法之间是平级关系
3.方法的执行顺序只和调用顺序有关系
4.void和[return 结果]不能共存,但是void能和return共存
  void:代表没有返回值
  return 结果:先将结果返回给调用处,然后结束方法
  return:仅仅代表结束方法
5.一个方法中,不能连续写多个return
6.调用方法的时候,要看看下面有没有这个方法(方法名,参数个数,参数类型都要匹配)
初学者怎么写:
1.先定义,再调用
2.如果是没有返回值的方法,直接在被调用的方法内输出;如果是带返回值的方法,就调用完毕之后,再写输出语句
3.调用方法:
  直接调用:方法名()  -> 针对的是无返回值的方法
      
  打印调用:System.out.println(方法名())->针对的是有返回值的方法
      
  赋值调用:数据类型 变量名  = 方法名()->针对的是有返回值的方法

练习:

1.如果定义的方法没有返回值,就写个void

2.如果定义的方法有返回值,就将void改成具体的数据类型,方法体中最后写一个return结果

3.如果方法有返回值,调用的时候就用赋值调用,打印调用

4.如果方法没有返回值,调用的时候就用直接调用

第十章.方法练习

快速将一段代码放到 一个方法中:了 ctrl+alt+m

1.方法练习1(判断奇偶性)

需求:
   键盘录入一个整数,将整数传递到另外一个方法中,在此方法中判断这个整数的奇偶性
   如果是偶数,方法返回"偶数"  否则返回"奇数"
       
参数:要
方法名:要
返回值:要
public class Test01 {
    public static void main(String[] args) {
       //创建Scanner对象
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        String result = method(i);
        System.out.println("result = " + result);
    }

    public static String method(int data){
        if (data%2==0){
            return "偶数";
        }else{
            return "奇数";
        }
    }
}

2.方法练习2(1-100的和)

需求 :  求出1-100的和,并将结果返回
参数:不要
方法名:要
返回值:要
public class Test02 {
    public static void main(String[] args) {
        //赋值调用
        int result = sum();
        System.out.println(result);
        //打印调用
        System.out.println(sum());
    }

    public static int sum(){
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        return sum;
    }
}

3.方法练习3(不定次数打印)

 需求:
    定义一个方法,给这个方法传几,就让这个方法循环打印几次"我是一个有经验的JAVA开发工程师"
 参数:要
 方法名:要
 返回值:不要
 public class Test03 {
     public static void main(String[] args) {
         print(5);
     }
 ​
     public static void print(int n){
         for (int i = 0; i < n; i++) {
             System.out.println("我是一个有经验的java开发工程师");
         }
     }
 }

4.方法练习4(遍历数组)

 需求:
   在main方法中定义一个数组,将数组传递到方法中,在此方法中遍历数组
 public class Test04 {
     public static void main(String[] args) {
         int[] arrA = {1,2,3,4};
         method(arrA);
     }
 ​
     public static void method(int[] arrB){//int[] arrB = arrA
         for (int i = 0; i < arrB.length; i++) {
             System.out.println(arrB[i]);
         }
     }
 }

image-20220219093840954

5.方法练习5(求数组最大值)

需求: 
  在main方法中定义数组,传递到另外一个方法中,在此方法中实现获取数组最大值
public class Test05 {
    public static void main(String[] args) {
        int[] arr = {3,2,5,5,4,6};
        int method = method(arr);
        System.out.println(method);
    }

    public static int method(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max<arr[i]){
                max = arr[i];
            }
        }

        return max;
    }
}

6.方法练习6(按照指定格式输出数组元素)

需求:
  1.定义一个数组 int[] arr = {1,2,3,4}
  2.遍历数组,输出元素按照[1,2,3,4]
public class Test06 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4};
        method(arr);
    }

    public static void method(int[] arr){
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i==arr.length-1){
                System.out.print(arr[i]+"]");
            }else{
                System.out.print(arr[i]+",");
            }
        }
    }
}

7.练习7

数组作为返回值返回
public class Test07 {
    public static void main(String[] args) {
        int[] arrB = method();
        for (int i = 0; i < arrB.length; i++) {
            System.out.println(arrB[i]);
        }
    }

    public static int[] method(){
        int a = 10;
        int b = 20;

        int sum = a+b;
        int sub = a-b;

        int[] arr = {sum,sub};
        return arr;
    }
}

image-20220219095200958

1.参数和返回值都是基本数据类型,传递和返回值的是数值

2.参数和返回值都是引用数据类型,传递和返回值的是地址值

第十一章.方法的重载

 1.需求:定义三个方法,实现2个整数相加,3个整数相加,4个整数相加
 public class Test01 {
     public static void main(String[] args) {
         sum(10,20);
         sum(10,20,30);
         sum(10,20,30,40);
     }
     //两个整数相加
     public static void sum(int a,int b){
         int sum = a+b;
         System.out.println(sum);
     }
     //三个整数相加
     public static void sum(int a,int b,int c){
         int sum = a+b+c;
         System.out.println(sum);
     }
 ​
     //四个整数相加
     public static void sum(int a,int b,int c,int d){
         int sum = a+b+c+d;
         System.out.println(sum);
     }
 }
 ​
 1.什么是重载方法:方法名相同,参数列表不同
 2.什么叫参数列表不同:
   参数个数不同
   参数类型不同
   参数类型的顺序不同
       
 3.重载方法和什么无关:
   a.和参数名无关
   b.和返回值无关
       
 4.什么时候可以定义重载方法:当功能一样,但是实现过程细节不同
 public static void open(){}
 public static void open(int a){}
 static void open(int a,int b){}
 public static void open(double a,int b){}
 public static void open(int a,double b){}
 public void open(int i,double d){}
 public static void OPEN(){}
 public static void open(int i,int j){}

image-20220219104406876

第十二章.debug

 1.作用:
   找bug  监视代码中数据值得变化  
 2.怎么用:
   a.想在哪一行上开始debug,就在这一行的左边单击一下,出现"小红点"(断点)
   b.右键-->debug运行

1627611738628

1622253604156

第十三章.类和对象

1.面向对象的介绍

 1.概述:java的中心思想  -> 自己的事情别人干   
        c语言思想:面向过程
       
       举例:洗衣服->洗衣机
           带饭
           点外卖
 2.为什么要使用面向对象思想->懒
   简化开发过程
   有很多功能,不需要自己写,别人都给我们实现了,我们只需要直接调用,这个功能就能直接实现,功能怎么实现的,不需要我们知道,我只知道调用就行了
           
 3.怎么用:
   a.new对象,点方法
   b.特殊:如果方法是带static的,我们不用new,直接类名点
       
 4.什么时候使用面向对象编程:
   当在一个类中,想使用别的类的功能,就要面向对象了,
   new对象,点方法
 public class Test01 {
     public static void main(String[] args) {
         int[] arr = {1,2,3,4};
         System.out.print("[");
         for (int i = 0; i < arr.length; i++) {
             if (i==arr.length-1){
                 System.out.print(arr[i]+"]");
             }else{
                 System.out.print(arr[i]+",");
             }
         }
         System.out.println();
         /*
           Arrays就是我们找来的对象
           toString就是这个对象中的功能
           我们直接调用Arrays对象中的toString方法就可以直接实现功能,toString怎么实现的,我们不用管
          */
         System.out.println(Arrays.toString(arr));
     }
 }
 ​

2.类和对象

2.1类(实体类)_class

 1.概述:一类事物的抽象表示形式
 2.定义:
   属性(成员变量):
       a.定义:数据类型 变量名
       b.成员变量是有默认值的,所以成员变量不用初始化就能使用,因为有默认值
         整数:0
         小数:0.0
         字符:'\u0000'
         布尔:false
         引用:null
       c.定义位置:类中方法外
       d.作用范围:整个类
   行为(成员方法):这一类事物都能干啥
       只需要干掉static,剩下的和之前学的一毛一样,调用一毛一样
 public class Person {
     //属性
     String name;
     int age;
 ​
     //行为
     public void eat(){
         System.out.println("人要吃饭");
     }
 ​
     public void drink(){
         System.out.println("人要喝水");
     }
 ​
     public void la(){
         System.out.println("人要拉屎");
     }
 ​
     public void sa(){
         System.out.println("人要嘘嘘");
     }
 }
 ​
 public class Phone {
     //属性
     String brand;//品牌
     double price;//价格
     String color;//颜色
     
     
     //行为
     public void call(){
         System.out.println("打电话");
     }
     
     public void message(){
         System.out.println("发短信");
     }
     
 }
 public class Animal {
     String kind;
     int age;
     
     public void eat(){
         System.out.println("吃饭");
     }
     
     public void sleep(){
         System.out.println("要睡觉");
     }
 }

2.2对象

 1.概述:一类事物的具体体现
 2.使用:
   a.导包:import 包名.包名.包名.类名
          当两个类在同一个包下,要是互相使用对方的成员(成员变量,成员方法),不用导包
          当两个类不在同一个包下,要是互相使用对方的成员(成员变量,成员方法),导包
          特殊包:lang包下的类使用时不用导包
              
   b.创建对象:想使用哪个类中的成员,就new哪个类的对象
     类名 对象名 = new 类名()
    
   c.调用成员(成员变量,成员方法):想使用哪个对象中的成员,就用哪个对象去点哪个成员
     对象名.成员变量 = 值
     对象名.方法名(实参)
 public class Phone {
     //属性
     String brand;//品牌
     double price;//价格
     String color;//颜色
 ​
 ​
     //行为
     public void call(){
 ​
         System.out.println("打电话");
     }
 ​
     public void message(){
 ​
         System.out.println("发短信");
     }
 ​
 }
 public class Test01 {
     public static void main(String[] args) {
         Phone phone = new Phone();
         phone.brand = "iPhone 13 pro max";
         phone.price = 8999;
         phone.color = "远峰蓝";
 ​
         System.out.println(phone.brand);
         System.out.println(phone.price);
         System.out.println(phone.color);
 ​
         phone.call();
         phone.message();
 ​
     }
 }

3.练习

 需求:用代码去描述一个动物类,在测试类中为动物类中的属性赋值,并且调用动物类中的功能
 public class Animal {
     String kind;
     int age;
 ​
     public void eat(){
         System.out.println("吃饭");
     }
 ​
     public void sleep(){
         System.out.println("要睡觉");
     }
 }
 ​
 public class Test02 {
     public static void main(String[] args) {
         Animal animal = new Animal();
         animal.kind = "二哈";
         animal.age = 3;
 ​
         animal.eat();
         animal.sleep();
     }
 }
 ​

小结:

1.什么使用面向对象思想:

在一个类中使用别的类的成员

2.怎么使用:

new对象,点成员

3.匿名对象的使用(注意)

 Person p = new Person()
 ​
 等号左边的Person:是等号右边对象的类型
 p:对象的名字
 new Person():创建对象
 1.概述:没有名字的对象
 2.格式: new 类名()
 3.注意:涉及到赋值了,不要用匿名对象
 public class Person {
     String name;
     public void eat(){
         System.out.println("吃饭");
     }
 }
 public class Test01 {
     public static void main(String[] args) {
         //原始做法
         Person person = new Person();
         person.name = "张三丰";
         person.eat();
 ​
         System.out.println("===========================");
 ​
         //匿名对象
         new Person().eat();
 ​
         System.out.println("=====================");
 ​
         new Person().name = "张无忌";
         System.out.println(new Person().name);//null
     }
 }
 ​

image-20220219145007732

4.一个对象的内存图

 public class Phone {
     String brand;
     int price;
     public void call(){
         System.out.println("打电话");
     }
 }
 public class Test01 {
     public static void main(String[] args) {
         Phone phone = new Phone();
         phone.brand = "小米";
         phone.price = 3499;
         phone.call();
     }
 }

image-20220219153512341

 自助:
 ​
   好伦哥:回龙观西大街旗舰店:99元
 ​
   铁木真:温都水城
 ​
 烧烤:
 ​
   平西府村口->兄弟烧烤
 ​
   平西府对过,温度水城->一错再错->2楼
 ​
 火锅:
 ​
    海底捞
 ​
    阳坊
 ​
    串串火锅:王小姐在成都
 ​
 虾:虾吃虾涮
 ​
 烤肉:回龙观西大街->焰遇
 ​
 海鲜自助:蚝英雄  初色 第六季
 ​
 簋街:胡大
 ​
 炒菜:金手勺

5.两个对象的内存图

image-20220219154510498

6.两个对象指向同一片空间内存图

image-20220219155158673

第十四章.成员变量和局部变量的区别

 1.定义位置(重点)
   成员变量:类中,方法外
   局部变量:方法内部或者参数位置
 2.作用范围(重点):
   成员变量:作用于整个类
   局部变量:作用于自己的方法
 3.初始化值(重点):
   成员变量:有默认值
   局部变量:没有默认值,必须初始化之后才能使用
 4.内存位置(了解):
   成员变量:堆中
   局部变量:栈中
 5.生命周期(了解):
   成员变量:随着对象的创建而创建,随着对象的消失而消失
   局部变量:随着方法被调用而创建,随着方法调用完毕而消失

第十五章.static静态关键字

1.static的介绍以及基本使用

image-20220221093407763

 1.概述:静态的,关键字
 2.特点
   a.被static修饰的成员随着类的加载而加载
   b.被static修饰的成员由于跟着类到内存,所以优先于对象存在
   c.被static修饰的成员属于类成员,不属于对象成员(成员方法,成员变量)
   d.根据static所在的类创建出来的对象,都会共享这个静态成员
 3.使用:
   修饰成员变量: static 数据类型 变量名
   修饰方法:
      修饰符 static 返回值类型 方法名(参数){
          方法体
          return 结果
      }
 4.使用:
   类名直接调用
 public class Student {
     String name;
     int age;
     static String classRoom;
 ​
     //非静态方法
     public void study(){
         System.out.println("学生要学习");
     }
 ​
     //静态方法
     public static void eat(){
         System.out.println("吃饭");
     }
 }
 ​
 public class Test01 {
     public static void main(String[] args) {
         //原始写法
         /*Student s1 = new Student();
         s1.name = "郭靖";
         s1.age = 26;
         s1.classRoom = "1111";
         System.out.println(s1.name);
         System.out.println(s1.age);
         System.out.println(s1.classRoom);
 ​
         System.out.println("=========================");
 ​
         Student s2 = new Student();
         s2.name = "黄蓉";
         s2.age = 20;
         s2.classRoom = "1111";
 ​
         System.out.println(s2.name);
         System.out.println(s2.age);
         System.out.println(s2.classRoom);*/
 ​
         //static用法
         //类名直接调用静态成员
         Student.classRoom = "2222";
 ​
 ​
         Student s1 = new Student();
         s1.name = "郭靖";
         s1.age = 26;
         //s1.classRoom = "1111";
         System.out.println(s1.name);
         System.out.println(s1.age);
         //System.out.println(s1.classRoom);
         System.out.println(Student.classRoom);
 ​
         System.out.println("=========================");
 ​
         Student s2 = new Student();
         s2.name = "黄蓉";
         s2.age = 20;
         //s2.classRoom = "1111";
 ​
         System.out.println(s2.name);
         System.out.println(s2.age);
         //System.out.println(s2.classRoom);
         System.out.println(Student.classRoom);
 ​
         System.out.println("=========================");
 ​
         //调用非静态方法
         Student s3 = new Student();
         s3.study();
 ​
         //调用静态方法
         Student.eat();
     }
 }

2.static修饰成员的访问特点

 1.在静态的成员中能直接访问非静态的成员吗? -> 不能
   如果想访问,new对象访问
     
 2.在非静态成员中能直接访问静态成员吗?->能
   a.在同一个类中:
     直接调用
     类名调用
     new对象调用
         
   b.不在同一个类中
     类名调用
     new对象调用
         
 3.在静态的成员中,能直接访问静态成员吗?->可以
   a.在同一个类中:
     直接调用
     类名调用
     new对象对象
   b.不在同一个类中:
     类名调用
     new对象调用
         
 4.在非静态成员中,能直接访问非静态成员吗?->可以
   a.在同一个类中:
     直接调用
     new对象调用
         
   b.在不同类中:
     new对象调用
     

总结:

1.凡是访问静态成员,都可以类名直接调用

2.凡是访问非静态成员,都可以new对象调用

第十六章.可变参数

 1.需求:
   定义一个方法,实现任意个整数相加
 2:
   参数:要
   方法名:要
   返回值:不要
 3:需求分析:
   参数的类型一致,确定
   参数的个数不一致,不确定

1介绍和基本使用

 1.什么时候使用可变参数:
   当只知道参数类型,不知道参数个数时,可以使用可变参数
 2.格式:
   数据类型...参数名
       
 3.本质:
   数组
       
 4.注意事项:
   a.参数位置只能有一个可变参数
   b.可变参数和普通参数可以一起出现,但是可变参数必须放到参数最后
 public class Test01 {
     public static void main(String[] args) {
         sum(1,2,3,4,53,2,4);
     }
 ​
     public static void sum(int...arr){
         int sum = 0;
         for (int i = 0; i < arr.length; i++) {
             sum+=arr[i];
         }
 ​
         System.out.println(sum);
     }
 }
 public class Test01 {
     public static void main(String[] args) {
         sum(1,2,3,4,53,2,4);
     }
 ​
     public static void sum(int a ,int...arr){
         int sum = 0;
         for (int i = 0; i < arr.length; i++) {
             sum+=arr[i];
         }
 ​
         System.out.println(sum);
     }
 }
 ​
 static <T> boolean addAll(Collection<? super T> c, T... elements) 将所有指定元素添加到指定 collection 中。 
 public class Test02 {
     public static void main(String[] args) {
         //创建集合对象 ArrayList
         ArrayList<String> list = new ArrayList<>();
         //添加元素
         /*list.add("关羽");
         list.add("张飞");
         list.add("刘备");*/
 ​
         Collections.addAll(list,"关羽","张飞","刘备","吕布");
 ​
         System.out.println(list);
 ​
     }
 }
 ​

2可变参数

JDK1.5之后,如果我们定义一个方法时,此时某个形参的类型可以确定,但是形参的个数不确定,那么我们可以使用可变参数。

格式:

 【修饰符】 返回值类型 方法名(【非可变参数部分的形参列表,】参数类型... 形参名){  }

要求:

(1)一个方法最多只能有一个可变参数

(2)如果一个方法包含可变参数,那么可变参数必须是形参列表的最后一个

(3)其实这个书写“≈”

 【修饰符】 返回值类型 方法名(【非可变参数部分的形参列表,】参数类型[] 形参名){  }

只是后面这种定义,在调用时必须传递数组,而前者更灵活,既可以传递数组,又可以直接传递数组的元素,这样更灵活了。

示例一: 求n个整数的和

 public class Test01 {
     public static void main(String[] args) {
         sum(1,2,3,4,53,2,4);
     }
 ​
     public static void sum(int a ,int...arr){
         int sum = 0;
         for (int i = 0; i < arr.length; i++) {
             sum+=arr[i];
         }
 ​
         System.out.println(sum);
     }
 }

示例二:字符串拼接

需求一:返回n个字符串拼接结果,如果没有传入字符串,那么返回空字符串""

 public class Test03 {
     public static void main(String[] args) {
         String concat = concat("java", "ui", "js", "bigdata");
         System.out.println("concat = " + concat);
     }
 ​
     public static String concat(String...arr){
         String str = "";
         for (int i = 0; i < arr.length; i++) {
             str+=arr[i];
         }
 ​
         return str;
     }
 }

需求二:n个字符串进行拼接,每一个字符串之间使用某字符进行分隔,如果没有传入字符串,那么返回空字符串""

 concat("-","柳岩","涛哥","翠儿")->  柳岩-涛哥-翠儿
 public class Test04 {
     public static void main(String[] args) {
         String concat = concat("-", "柳岩", "涛哥", "翠儿");
         System.out.println("concat = " + concat);// 柳岩-涛哥-翠儿
     }
 ​
     public static String concat(String s, String... arr) {
         String str = "";
         for (int i = 0; i < arr.length; i++) {
             if (i == arr.length - 1) {
                 /*
                    str = str+arr[i]
                    str = str+arr[2]-> 柳岩-涛哥-+"翠儿"-> 柳岩-涛哥-翠儿
                  */
                 str += arr[i];
             }else{
                 /*
                   str = str+arr[i]+"-"   str = ""+"柳岩"+"-"-> str = 柳岩-
                   
                   str = str+arr[1]+"-"  str = 柳岩-+"涛哥"+"-"-> 柳岩-涛哥-
                  */
                 str += arr[i]+s;
             }
         }
         return str;
     }
 }
 ​

第十七章.递归

 从前有座山,山上有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的啥?
       从前有座山,山上有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的啥?
           从前有座山,山上有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的啥?
               从前有座山,山上有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的啥?...
     
 老和尚没了,庙塌了,小和尚结婚了
     
 1.概述:方法内部自己调用自己
 2.分类:
   a.直接递归:
     public static void method(){
        method();
     }
   b.间接递归:
     public static void A(){
        B();
     }
 ​
     public static void B(){
        C();
     }
 ​
     public static void C(){
        A();
     }
 3.注意:
   a.递归必须要有出口
   b.递归即使有出口,也不能递归太多次

1 递归

  • 递归:指在当前方法内调用自己的这种现象。

  • 递归的分类:

    • 递归分为两种,直接递归和间接递归。
    • 直接递归称为方法自身调用自己。
    • 间接递归可以A方法调用B方法,B方法调用C方法,C方法调用A方法。
  • 注意事项

    • 递归一定要有条件限定,保证递归能够停止下来,否则会发生栈内存溢出(因为会不断的压栈)。
    • 在递归中虽然有限定条件,但是递归次数不能太多。否则也会发生栈内存溢出。

示例一:需求:利用递归输出3到1

 public class Test01 {
     public static void main(String[] args) {
         method(3);
     }
 ​
     public static void method(int n){
         //出口
         if (n==1){
             System.out.println(1);
             return;
         }
         System.out.println(n);
         n--;
         method(n);
     }
 }
 ​

示例二:求n!

1616663476109

 1*2*3
 ​
 method(int n)->代表的是n的阶乘
     
 method(1)   1
 method(2)   1*2->  method(1)*2
 method(3)   1*2*3->method(2)*3
     
 n的阶乘->  method(n-1)*n
 public class Test02 {
     public static void main(String[] args) {
         int result = method(3);
         System.out.println(result);
     }
     public static int method(int n){
         if (n==1){
             return 1;
         }
         return n*method(n-1);
     }
 }

image-20220221115117470

示例三:计算斐波那契数列(Fibonacci)的第n个值

 不死神兔
 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡
 问:一对刚出生的兔子,一年内繁殖成多少对兔子? 

规律:一个数等于前两个数之和,比如: 1 1 2 3 5 8 13 21 34 55....

image-20220221141154223

 1.定义一个方法,参数为月份-> method(n)
 2.分析规律
   method(1)    1
   method(2)    1
   method(3)    2 ->method(1)+method(2)
   method(4)    3 ->method(2)+method(3)
   method(5)    5 ->method(3)+method(4)
     
   method(n)-> method(n-2)+method(n-1)
 public class Test03 {
     public static void main(String[] args) {
         int result = method(12);
         System.out.println("result = " + result);
     }
 ​
     public static int method(int n) {
         if(n==1 || n==2){
             return 1;
         }
         return method(n-2)+method(n-1);
     }
 }

第十八章.对象数组

 需求:定义一个长度为3的数组,存储3个Person对象  遍历数组,将Person对象的属性值获取出来
 public class Person {
     String name;
     int age;
 }
 public class Test01 {
     public static void main(String[] args) {
         /*
           1.数组中存储int型的整数:  int[]
           2.数组中存储double型的小数: double[]
           3.数组中存储String型的字符串: String[]
           4.数组中存储long型的整数:long[]
           5.数组中存储Person类型的Person对象:Person[]
          */
         //定义数组
         Person[] arr = new Person[3];
         //创建Person对象
         Person p1 = new Person();
         p1.name = "樱桃小丸子";
         p1.age = 12;
 ​
         Person p2 = new Person();
         p2.name = "百变小樱";
         p2.age = 11;
 ​
         Person p3 = new Person();
         p3.name = "熊二";
         p3.age = 15;
 ​
         //存储数组
         arr[0] = p1;
         arr[1] = p2;
         arr[2] = p3;
 ​
         /*
           第一圈循环:arr[0] = p1      Person p = p1;
           第二圈循环:arr[1] = p2      Person p = p2;
           第三圈循环:arr[2] = p3      Person p = p3;
          */
         for (int i = 0; i < arr.length; i++) {
             Person p = arr[i];
             System.out.println(p.name+"..."+p.age);
         }
     }
 }
 ​

image-20220221143029900

数组是用来存储一组数据的容器,一组基本数据类型的数据可以用数组装,那么一组对象也可以使用数组来装。

即数组的元素可以是基本数据类型,也可以是引用数据类型。当元素是引用数据类型时,我们称为对象数组。

注意:对象数组,首先要创建数组对象本身,即确定数组的长度,然后再创建每一个元素对象,如果不创建,数组的元素的默认值就是null,所以很容易出现空指针异常NullPointerException。

将代码快速抽取到一个方法中:ctrl+alt+m

第十九章.二分查找(折半查找)

 1.前提:数组中的元素必须是有序
 2.查找思想:每次查找都少查一半
 3.目的:提高查找效率

image-20220221153319091

 public class Test01 {
     public static void main(String[] args) {
         //定义数组
         int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
         //定义一个最大索引和最小索引
         int min = 0;
         int max = arr.length - 1;
         //定义一个要查找的key
         int key = 10;
 ​
         while (min <= max) {
             //定义中间索引
             int mid = (min + max) / 2;
             if (key>arr[mid]){
                 min = mid+1;
             }
 ​
             if (key<arr[mid]){
                 max = mid-1;
             }
 ​
             if (key==arr[mid]){
                 System.out.println(mid);
                 break;
             }
         }
     }
 }
 ​

第二十章.数组翻转

 1.数组中对称索引位置上的元素互换位置

image-20220221154805733

 public class Test01 {
     public static void main(String[] args) {
         int[] arr = {1,2,3,4,5};
         for (int min = 0,max = arr.length-1;min<max;min++,max--){
             int temp = arr[min];
             arr[min] = arr[max];
             arr[max] = temp;
         }
 ​
         System.out.println(Arrays.toString(arr));
     }
 }

问题:

char类型的数组直接输出数组名不是地址值

原因:底层实现的,println方法实现的

第二十一章.方法参数

1.基本数据类型做方法参数传递

 基本类型作为方法参数传递,传递的是值
 public class Test01 {
     public static void main(String[] args) {
         int a = 10;
         int b = 20;
         method(a,b);
         System.out.println(a);//10
         System.out.println(b);//20
     }
 ​
     public static void method(int a,int b){
         a+=10;
         b+=20;
         System.out.println(a);//20
         System.out.println(b);//40
     }
 }

image-20220221161622050

2.引用数据类型做方法参数传递

 引用数据类型作为方法参数传递,传递的是地址值
 public class Test02 {
     public static void main(String[] args) {
         int[] arr = {10,20};
         method(arr);
         System.out.println(arr[0]);//20
         System.out.println(arr[1]);//40
     }
 ​
     public static void method(int[] arr) {
         arr[0]+=10;
         arr[1]+=20;
         System.out.println(arr[0]);//20
         System.out.println(arr[1]);//40
     }
 }
 ​

image-20220221162154588

第二十二章.命令行参数(了解)

通过命令行给main方法的形参传递的实参称为命令行参数

1616663442452

 public class TestCommandParam{
     //形参:String[] args
     public static void main(String[] args){
         for(int i=0; i<args.length; i++){
             System.out.println("第" + (i+1) + "个参数的值是:" + args[i]);
         }
     }
 }

运行命令:

 java TestCommandParam
 java TestCommandParam 1 2 3
 java TestCommandParam hello atguigu

第二十三章.API文档的使用

 1.API概述:类,接口,以及其中的方法
 2.根据API编出一个API文档:程序员的"字典"
 3.作用:查一查
 4.什么时候使用:查一下某个类中的方法

1.查找想要使用的API

image-20220221163636645

2.看对应API的成员

image-20220221163842972