Java Foundation (array)

array

  • An array is a collection of multiple data of the same type arranged in a certain order, named with a name, and managed uniformly by numbering.

  • Common concepts of arrays
    1. Array name
    2. Subscript (or index)
    3. Elements
    4. Length of array: number of elements

  • Characteristics of array
    Arrays are ordered

The array itself is a reference data type, and the elements in the array can be any data type, including basic data type and reference data type.

Creating an array object will open up a whole contiguous space in memory, and the first address of this contiguous space is referenced in the array name.

Once the length of the array is determined, it cannot be modified.

You can directly call the element at the specified position by subscript (or index), which is very fast.

  • Classification of arrays
    1. According to dimension: one-dimensional array, two are arrays
    2. According to the type of array element, the array of basic data type elements and the array of reference data type elements

  • Default initialization value for array elements
    1. Array element is integer: 0
    2. Array elements are floating point: 0.0
    3. Array elements are char type, 0 or '\ u0000', not '0'
    4. Array elements are boolean, false
    5. The array element is a reference data type: null

java memory composition

Multidimensional array

  • If a one-dimensional array can be regarded as a linear figure in geometry, then a two-dimensional array is equivalent to a table.
  • For the understanding of two-dimensional arrays, we can regard them as a pile of arrays array1 and as elements of another one-dimensional array array2. In fact, from the perspective of the underlying operation mechanism of the array, there is no multi-dimensional array.
public class ArrayTest2 {
    public static void main(String[] args) {
        //1. Declaration and initialization of two-dimensional array
        // Static initialization 1
        int [][] arr1 = new int[][] {{1,2,3},{4,5},{6,7,8}};
        
        // Static initialization 2
        int[] arr4[] = new int[][] {{1,2,3},{4,5},{6,7,8}};
        
        // Static initialization 3
        int[] arr5[] = {{1,2,3},{4,5},{6,7,8}};
        
        // Dynamic initialization 1
        String[][] arr2 = new String[3][2];
        
        // Dynamic initialization 2
        String[][] arr3 = new String[3][];
        
        //2.How to call an element at a specified position of an array
        System.out.println(arr1[0][1]);    //2
        System.out.println(arr2[1][1]);    //null
        //System.out.println(arr3[1][0]);  //report errors
        
        arr3[1] = new String[4];
        System.out.println(arr3[1][0]);    //null
        
        //3.Gets the length of the array
        System.out.println(arr4.length);   //3
        System.out.println(arr4[0].length);//3
        
        //4.Traversing a two-dimensional array
        for(int i = 0; i < arr4.length; i++) {
            for(int j = 0; j < arr4[i].length; j++) {
                System.out.print(arr4[i][j] + " ");
            }
            System.out.println();
        }
    }

}

Two dimensional array

  • A two-dimensional array is divided into elements of an outer array and elements of an inner array
    For example: int [][] arr = new int[4][3];
    Outer elements: arr[0], arr[1], etc
    Inner element: arr[0][0], arr[1][2]
  • Default initialization value for array elements
    For initialization method 1: for example, int[][] arr = new int[4][3];
    The initialization value of the outer element is: address value
    The initialization value of the inner element is: the same as that of the one-dimensional array
    For initialization method 2: for example, int[][] arr = new int[4] [];
    The initialization value of the outer element is null
    The initialization value of the inner element is: it cannot be called, otherwise an error is reported

Two dimensional array memory parsing

public class ArrayTest3 {
    public static void main(String[] args) {
        int [][] arr = new int[4][3];
        System.out.println(arr[0]);        //[I@182decdb  <- Unary array address value
        System.out.println(arr[0][0]);     //0
        System.out.println(arr);           //[[I@7637f22  <- Binary array address value
        
        System.out.println("********************************");
        float[][] arr1 = new float[4][5];
        System.out.println(arr1[0]);       //[F@4926097b
        System.out.println(arr1[0][0]);    //0.0
        
        System.out.println("********************************");
        String[][] arr2 = new String[4][2];
        System.out.println(arr2[1]);       //[Ljava.lang.String;@2d363fb3
        System.out.println(arr2[1][1]);    //null
    }

}

 

Keywords: Java

Added by gogul on Mon, 29 Nov 2021 07:34:55 +0200