Java Basics-5

Five, Java array

1. Overview of arrays

          > Array ( Array ), is a collection of multiple data of the same type sorted in a certain order, named with a name, and unified management of these data by numbering

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

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

          > Array is a container that stores multiple elements of the same data type

          > Array can store both basic data types and reference data types

          >The classification of the array:

                   a. According to dimensions: one-dimensional array, two-dimensional array, three-dimensional array,...

                   b. According to the data type of the element: an array of basic data type elements, an array of reference data type elements (that is, an array of objects)

2. The use of one-dimensional arrays

    2.1 How to declare a one-dimensional array:

          type var [] 或 type [] var;

        > Format 1: Data type [] array name;

        > Format 2: Data type array name [];

          E.g:

                    int a [];

                    int[] a1;

                    double b[];

                    String[] c;//reference variable array

          When declaring an array in the    Java language, you cannot specify its length (the number of elements in the array), for example: int a[5];// Illegal

     2.2 The use of one-dimensional array: initialization

                    2.2.1 Overview of initialization:

                    Arrays in Java must be initialized before they can be used

                    Initialization: Allocate memory space for the array elements in the array, and assign values ​​to each array element

           a. Dynamic initialization: the operation of array declaration and allocating space for array elements and assignment is carried out separately

                           Format: data type [] array name = new data type [array length];

                                    The length of the array is actually the number of elements in the array

           b. Static initialization: Specify the initial value of each array element during initialization, and the length of the array is determined by the system

                           Format: data type [] array name = {initial value 1, ..., initial value n};

                                          Such as: int [] arr = {1,3,5,7,9};

    2.3 The use of one-dimensional arrays: references to array elements

          a. After defining and using the operator new to allocate space for it, each element in the array can be referenced

          b. Reference method of array elements: array name [array element subscript]

                     >The subscript of the array element can be an integer constant or an integer expression. Such as a[2],b[i],c[6*i]

                     >The subscript of the array element starts from 0; the valid subscript value range of an array of length n: 0 ->n-1;

                               For example, int a [] = new [3]; The array elements that can be quoted are a[0], a[1], a[2]

          c. Each array has an attribute length indicating its length, for example: a.length indicates the length of array a (number of elements)

                     >Once the array is initialized, its length is immutable

     2.4 The use of one-dimensional arrays: the default initialization value of the array elements

         The array is a reference type, and its elements are equivalent to the member variables of the class, so once the array is allocated space, each element in it is implicitly initialized in the same way as the member variable. For example:

public class ArrayTest{

    public static void main(String args[]){
           int [] arr = new int[5];
            System.out.println(arr[3]);// arr[3]的默认值为0
    }    
}

             >For basic data types, the default initialization values ​​are different

             >For reference data types, the default initial value is null (different from 0)

     2.5 Use of one-dimensional arrays-create arrays of basic data types 

          >Use the keyword new in Java to create an array 

          > The following is to create a one-dimensional array of basic data type elements 

public class Test{
    public static void main(String args[]){
        int [] s;
        s = new int [10];
        //遍历数组
        for(x = 0;x<s.length;x++){
            System.out.println(s[x]);
        }    
    }
    
}

3. The use of multidimensional arrays

    3.1 The concept of multidimensional arrays

          A one-dimensional array has only one subscript. An array with two or more subscripts is called a multidimensional array. Multi-dimensional array elements have multiple subscripts to identify their position in the array. The description of multi-dimensional arrays is basically similar to the description of one-dimensional arrays, and the general form of the description is as follows:

          <Storage Type><Data Type><Array Name><Constant Expression 1><Constant Expression 2>...<Constant Expression n>

   3.2 The use of multi-dimensional array-two-dimensional array

         >Two-dimensional array [][]: the array in the array

        3.2.1 Dynamic initialization 

        a. Format 1 (dynamic initialization): int [][] arr = new int [3][2];

            >A two-dimensional array named arr is defined

            There are 3 one-dimensional arrays in the two-dimensional array

            There are 2 elements in each one-dimensional array

            >The names of a one-dimensional array are arr[0], arr[1], arr[2]

        b. Format 2 (dynamic initialization): int [][] arr = new int[3][];

            There are 3 one-dimensional arrays in the two-dimensional array

         Each one-dimensional array is the default initialization value null (note: difference format 1)

            > These are three one-dimensional arrays can be initialized separately

            arr[0] = new int [3]; arr[1] = new int[1] ;  arr[2] = new int [2] ;       

       Note: int [][] arr = new int [][2];//Illegal

    3.2.2 Static initialization

      Format ( static initialization ): int [][] arr = new int{ {3,2,1},{2,7},{9,0,1,6}};

         >Define a two-dimensional array named arr, there are three one-dimensional arrays in the two-dimensional array

         > Each specific element in a one-dimensional array has been initialized

         >The first one-dimensional array arr[0] = {3,2,1};

         >The second one-dimensional array arr[1] = {2,7};

         >The third one-dimensional array arr[2] = {9,0,1,6};

         >The length representation of the third one-dimensional array: arr[2].length;

          Note: Special case: int [] x,y[]; x is a one-dimensional array, and y is a two-dimensional array.

                  Multidimensional arrays in Java do not have to be in regular rectangular form

 

 

 

 

 

 

 

 

 

 

 

 

               

 

 

 

 

 

 

 

 

 

 

 

 

 

          

Guess you like

Origin blog.csdn.net/weixin_52011642/article/details/109443657