[Array Knowledge Points]


1. Overview of arrays and static initialization

Introduction to Arrays

Array refers to a container that can be used to store multiple values ​​of the same data type

  • When an array container stores data, it needs to be considered in combination with implicit conversion
  • Example: array container of int type (byte, short, int can be stored)
  • Example: double type array container (byte, short, int, long, float, double can be stored)

array definition

The first:

data type [ ] array name
int [ ] array

The second type:

Data type array name[ ]
int array[ ]

Static initialization of arrays

  • Initialization: It is the process of opening up space for the array container in memory and storing the data in the container
  • Complete format: data type [] array name = new data type [] {element 1, element 2, element 3...};
  • Simplified format: data type [] array name = {element 1, element 2, element 3...};
  • Example: int[] array = {11,22,33};
  • Example: double[] array2 = {11.1,22.2,33.3};
  //定义学生年龄
    int[] array = new int[]{
    
    11, 22, 33};
    int[] array1 ={
    
    11, 22, 33}; //简写格式
    //定义学生身高
    double[] array2 = new double[]{
    
    1.85, 1.86, 1.87};
    double[] array3 = {
    
    1.85, 1.86, 1.87};
    //定义学生姓名
    String[] array4 = new String[]{
    
    "张三", "李四", "王五"};
    String[] array5 = {
    
    "张三", "李四", "王五"};

2. Array address value and element access

The address value of the array: indicates the location of the array in memory

 int[] array1 ={
    
    11, 22, 33}; //简写格式
 System.out.println(array1);//地址值:[I@776ec8df
        //[:表示当前是一个数组
        //I:表示当前数组里面的元素都是int类型的
        //@:表示一个间隔符号
        //776ec8df:数组真正的地址值(十六进制)

element access

arrayName[index];

index:

  • Index: also called subscript, corner mark.
  • Features: start from 0, increase by +1 one by one, continuous and uninterrupted
public class array {
    
    
    public static void main(String[] args) {
    
    
        //利用索引对数组中的元素进行访问
        int[] arr ={
    
    1, 2, 3, 4, 5};
        int number = arr[0];
        System.out.println(number);

        //把数据存储到数组中
        arr[0] = 100;
        //一但被覆盖后,原来的数据就不存在了
        System.out.println(arr[0]);
    }
}

Third, the traversal of the array

Use loops to quickly traverse arrays
In Java, regarding a length attribute of an array,
the calling method of length is: array name.length

public class array {
    
    
    public static void main(String[] args) {
    
    
        int[] array ={
    
    1, 2, 3, 4, 5};
        //利用循环取出元素
        for(int i = 0; i < array.length; i++){
    
    
            System.out.println(array[i]);
        }
    }
}

exercise 1

Define an array 1, 2, 3, 4, 5, and sum

public class array {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1,2,3,4,5};
        //定义求和变量
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
    
    
            //i是数组中的索引
            //arr[i]是数组中的元素
            count = count + arr[i];
        }
        System.out.println(count);
    }
}

exercise 2

Define an array 1 to 10, and count how many numbers are divisible by 3

public class array {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        //定义统计变量
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
    
    
           if(arr[i] % 3 == 0){
    
    
               count++;
           }
        }
        System.out.println(count);
    }
}

Exercise 3

Modify the elements in the array during traversal.
Define an array of 1-10.
If it is an even number, it will be half of the current number. If it is an odd number, it will be doubled.

public class array {
    
    
    public static void main(String[] args) {
    
    
        int[] arr ={
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        for (int i = 0; i < arr.length; i++) {
    
    
            if(arr[i] % 2 == 0){
    
    
                arr[i] = arr[i] / 2;
            }else{
    
    
                arr[i] = arr[i] * 2;
            }
        }
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.println(arr[i]);
        }
    }
}

Fourth, the dynamic initialization of the array

Dynamic initialization: only the length of the array is specified during initialization, and the system assigns the initial value to the array

  • Format: data type [] array name = new data type [array length];
  • Example: int[] arr = new int[3];
    define a dynamically initialized array and assign
public class array {
    
    
    public static void main(String[] args) {
    
    
        //假如新生有50人来报道,暂时不知道姓名,所以定义一个动态数组
        String[] arr = new String[50];
        //进行赋值
        arr[0] = "张三";
        arr[1] = "李四";
        System.out.println(arr[0]);//张三
        System.out.println(arr[1]);//李四
        System.out.println(arr[2]);//null-动态初始化值
    }
}

The law of the default initialization value of the array

  • Integer type: default initialization value 0
  • Decimal type: default initialization value 0.0
  • Character type: default initialization value '/u0000' space
  • Boolean type: the default initialization value is false
  • Reference data type: default initialization value null

Difference between static initialization and dynamic initialization

Dynamic initialization: manually specify the length of the array, and the default initialization value is given by the system

  • Only the number of elements is specified, but the specific value is not specified. It is recommended to use dynamic initialization

Static initialization: Manually specify the array elements, and the system will calculate the length of the array based on the number of elements

  • The specific data to be operated has been specified in the requirements, and the static initialization can be done directly

Guess you like

Origin blog.csdn.net/qq_64451048/article/details/127667902