Java learning road-array

Java learning road-array

One, array overview

  • Array (Array) is an ordered sequence of elements. Array is a form of organizing several elements of the same type in an orderly form in program design. These ordered collections of similar data elements are called arrays;

  • The array itself is a reference type, and the elements in the array can be of any type (including basic data types and reference data types);

  • Once the length of the array is determined, it cannot be changed;

  • The array elements can be accessed by index.

Two, initialize the array

1. Static initialization

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[]{
    
    1001, 1002, 1003};;  // 数组的声明和赋值同时进行
    }
}

2. Dynamic initialization

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[3];  // 数组的声明和赋值分开进行,动态初始化时声明传递数组长度
		
        arr[0] = 1001;
        arr[1] = 1002;
        arr[2] = 1003;
    }
}

Third, the traversal of array elements

1. Index

Each array element can be accessed directly by index, but there is too much repetitive work when the array length is large.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[]{
    
    1001, 1002, 1003, 1004};  // 声明一个数组变量

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

2. Loop + Index

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[]{
    
    1001, 1002, 1003, 1004};  // 声明一个数组变量

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

3. for each loop

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[]{
    
    1001, 1002, 1003, 1004};  // 声明一个数组变量

        for (int i: arr) {
    
    
            System.out.println(i);
        }
    }
}

4. Arrays.toString() method

import java.util.Arrays;

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[]{
    
    1001, 1002, 1003, 1004};  // 声明一个数组变量

        System.out.println(Arrays.toString(arr));
    }
}

Fourth, the initial value of the array

When the array is initialized, an initial value is assigned inside the array. The initial value of different types of arrays is different:

import java.util.Arrays;

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        // 整型
        byte[] arr1 = new byte[3];
        short[] arr2 = new short[3];
        int[] arr3 = new int[3];
        long[] arr4 = new long[3];

        // 浮点型
        float[] arr5 = new float[3];
        double[] arr6 = new double[3];

        // 非数值型
        char[] arr7 = new char[3];
        boolean[] arr8 = new boolean[3];

        // 字符串
        String[] arr9 = new String[3];

        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println(Arrays.toString(arr3));
        System.out.println(Arrays.toString(arr4));
        System.out.println(Arrays.toString(arr5));
        System.out.println(Arrays.toString(arr6));
        System.out.println(Arrays.toString(arr7));
        System.out.println(Arrays.toString(arr8));
        System.out.println(Arrays.toString(arr9));
    }
}

result:

[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0.0, 0.0, 0.0]
[0.0, 0.0, 0.0]
[ ,  ,  ]
[false, false, false]
[null, null, null]

It can be found from the output result:

  • The initial values ​​of byte, short, int, and long integer arrays are 0 ;
  • The initial values ​​of float and double float arrays are 0.0 ;
  • The initial value of the char type array is '\u0000' (not nothing);
  • The initial value of boolean array is false ;
  • The initial value of String type array is null .

Five, multi-dimensional array

1. Two-dimensional array

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        // 初始化二维数组
        int[][] arr = new int[3][];  // 两个方括号中第一个方括号中必须指定数组长度
        arr[0] = new int[]{
    
    1001, 1002, 1003};
        arr[1] = new int[]{
    
    1004, 1005};
        arr[2] = new int[]{
    
    1006, 1007, 1008, 1009};

        // 遍历二维数组
        for(int i=0 ; i<arr.length ; i++) {
    
    
            for (int j=0; j<arr[i].length ; j++) {
    
    
                System.out.println(arr[i][j]);
            }
        }
    }
}

2. Multidimensional array

It can be promoted by a two-dimensional array.

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112335328