Java zero-based study notes (Day05)

Overloading a method of

        Concept: In a class with the same name multiple methods, different parameter list of the method of phenomenon is called method overloading

        Characteristics :

1, in the same class

2, the same method name

3, different parameter list

        A list of different parameters:

1, the number of different parameters

2, parameters of different types of the same number of parameters [premise]

3, a different order of the parameters [identical number of parameters, parameter types have not the same type]

        : Characteristics return type, a method thereof, and methods of modifiers does not matter

Second, the array

        Overview:

1, he is a reference to a data type of

2, the array is used to store a plurality of containers of the same data type

3, the length of the array immutable once created.

       Initialize an array of two kinds initialized []

        Initialized:

1, static initialization: Create an array out when we want to put the data into the array

Standard format: Data Type [] = new Array Name Data Type [] {value 1, value 2} .....

Simplified format: Data Type [] array name = {value 1, value 2 .....}] Common

2, dynamic initialization: Create an array of time did not want to put value into the array, the value of a given length

Format: Data Type [] = new Array name Data type [length of the array];

Explanation:

Data types: basic data types can also be a reference data types

[]: It is the standard symbol array.

new: keywords heap space open up a new space out

      The sample code

com.ujiuye.demo Package; 

public  class Demo02 {
     public  static  void main (String [] args) {
         // static initializer 
        int [] = ARR new new  int [] { . 1 , 2 , . 3 , . 4 , . 5 }; // . 5 length of the array 
        . the System OUT .println (ARR); // [@ 15db9742 the I 
        int [] = {of arr1 . 6 , . 7 , . 8 }; // array of length three 
        . the System OUT .println (of arr1); //[6d06d69c the I @
         // dynamic initialization 
        String [] = arr2 is new new String [ . 4 ]; // . 4 length string array size 
        . The System OUT .println (arr2 is); // [Ljava.lang.String; @ 7852e922 
    } 
}

 

          [I @ 15db9742: address of the array [address exists an array of open space in the heap value]

          [:  Representation is one-dimensional array

          I: indicates the data type of data store array

         @: It is a connector

         15db9742: hash value [open space when the space system to generate a hexadecimal number]

        An array of memory understanding

How is an array to store data?

The space inside the array into a position a little small space [], each location storing a data,

How long will the number of array locations.

An array of memory map:

​ 

FIG two arrays of memory:

​ 

Two reference points to an array of memory map:

Quote: array variable is the address of an array of values

​ 

         Using an array of

          Overview: When using an array operation is the address of an array of values.

​          使用步骤:

​                    1、使用数组变量名【数组的地址值】找到堆空间中对应的数组空间

​                    2、通过数组空间的位置的编号来找到对应的位置

​                    3、就可以操作该位置的数据

​          数组空间的位置编号官方起名叫做索引【下标、角标】,是从0开始的。

​          索引的最大值比数组的长度值小1

          取出数据:

​          格式:数组名[索引值] 比如:arr[3]

          存放数据:

​          格式:数组名称[索引值] = 值; 比如:arr[3] = 5;

          代码示例

package com.ujiuye.demo;

public class Demo03 {
    public static void main(String[] args) {
        String[] arr = {"金莲","百合","蓉蓉","小鹿","冬施"};
        //取值   蓉蓉  索引值  0 1 2 3 4
        System.out.println(arr[2]);//蓉蓉
        //赋值   把冬施这个位置放上蔡徐坤
        System.out.println(arr[4]);//冬施
        arr[4] = "蔡徐坤" ;
        System.out.println(arr[4]);//蔡徐坤
    }
}

 

         数组的操作说白了就是使用数组的索引值进行操作。操作的索引值必须要在数组索引范围内;

         操作数组首先要找到对应的数组的地址值,要求数组操作的时候必须要有具体的地址值

         数组的异常:

                   角标【索引】越界异常:【ArrayIndexOutOfBoundsException】

​                   原因:操作数组是使用的索引值不在该数组的索引值范围内造成的

​                   该异常编译不会报错,只有在代码运行的时候才会报错【运行异常】

                   空指针异常:【NullPointerException】

​                   原因:操作的数组变量中存放的是null值,没有具体的地址引用,找不到要操作的数组

​                   前提:操作地址值为null的数组变量去存取值才会发生

           数组遍历

                概述:一次性的从数组中获取多个数据。数组的数据获取通过索引值获取,一个一个的去获取。

                获取多个数据是一个重复动作。使用循环来控制索引值的变化,通过索引值依次获取对应的数据这样的操作叫做遍历。

                遍历的过程中利用索引的最大值比长度值小1 规律作为遍历的条件

                数组的长度获取:数组名称.length;使用for循环进行取值就是数组的遍历

                代码示例:

package com.ujiuye.demo;

public class Demo04 {
    public static void main(String[] args) {
        //创建一个字符串数组
        String[] arr = {"宝宝","乃亮","大朗","羽凡"};
        //遍历 初始化变量 表示索引
        for (int i = 0; i < arr.length; i++) {
            //开始取值
            System.out.println(arr[i]);
        }
    }
}

 

       练习

        1、获取一个int类型数组中数据的最大值

          分析:求取多个int数据的最大值; 采用打擂台的思想来求取最大值

​              1、从这些数据中随便抽取一个作为第一个擂主

​              2、要让其他的所有元素一次的和擂主打擂

​                   打输了不管下一个挑战

​                   打赢了变成新的擂主,同下一个元素继续挑战

​                   一直到所有的元素挑战完毕,最终的擂主就是最大值

            代码示例

package com.ujiuye.demo;

public class Test01 {
    public static void main(String[] args) {
        int[] arr = {123,34,67,23,234,573,2238};
        //假设一个擂主  arr[0]
        int max = arr[0];
        //进行打擂  获取其他的每一个元素
        //遍历获取每一个元素
        for (int i =  1; i < arr.length; i++) {
            //打擂  arr[i]是挑战者   max 就是擂主
            if (max < arr[i]) {
                //条件成立擂主失败  换擂主
                max = arr[i];
            }
        }
        //循环完成之后就是打擂结束了  max里面就是最后的擂主也就最大值
        System.out.println("数组arr中的最大值是:"+max);
    }
}

 

        2、将数组的元素进行首尾反转

​            分析:

            代码示例:


        3、数组元素索引的查找

      1、给定一个数组,找到指定值的索引
      2、思路:使用要查找的值取和数组的元素取对比,相同了找到了,把对应的元素的索引值返回。

          代码示例

Guess you like

Origin www.cnblogs.com/nastu/p/12323812.html