The concept of two-dimensional arrays and code blocks

2D array :

datatype][ ] arrayname = new datatype[m][n] ;

m: represents how many one-dimensional arrays are in the current two-dimensional array

n: represents the length of each one-dimensional array


There are two ways to define :

datatype [] arrayname[] = new datatype[m][n] ;

data type array name [ ][ ] = new data type [m][n] ;


For example the concrete type defined :

int x;

int x,y ;

int[ ] x ;

int [ ] x,y[ ];

The second format of the two-dimensional array definition :

DataType [ ][ ] ArrayName = new DataType[m][ ]; 

Only m one-dimensional arrays are given, and the length of each one-dimensional array is dynamically given .

 

Two-dimensional array format 3

static initialization

datatype [ ][ ] array name = {{element1,element2,element3},{....}};

 

Given a two-dimensional array , statically initialize , iterate over each element in it

    for(int x = 0 ; x < arr.length ; x ++) {

        for(int y = 0 ; y < arr[x].length; y++) { 

            System.out.print(arr[x][y]+" ");

                    }

  }

 

An overview of code blocks :

Codes enclosed in { } are collectively referred to as code blocks;

According to its location and declaration , it is divided into the following :

    Local code block : in main() , define the life cycle of the variable

    Constructing code blocks : In a member position in a class, enclose it with {},

    Function : You can put the same code in multiple construction methods into the construction code block to initialize the object. Before each execution of the construction method , the construction code block is executed first .

    Static code block : In the member position of a class, it is also wrapped with {}, but it is modified by static .

    Role : In general, its role is to initialize the class .

Here is an interview question :

Constructor code block , constructor method, priority of static code?

Static code block > Constructor code block > Constructor method

Static code : can only be executed once .

The constructor block is executed before each execution of the constructor .

 

Method override :

Since the subclass provides the same method declaration when inheriting the parent class , the method of the parent class will be overwritten (overridden, overwritten)

Sometimes (specific requirements), subclasses do not need to override the functions of the parent class. For this situation, Java provides a keyword: final , final , final meaning


final can modify not only basic data types, but also reference types

If the final modification is a basic data type: the value of the basic data type can no longer be changed ,

If the final modification is a reference type data : the address value of the reference type can no longer be changed, but the value of the member variable in the heap memory can be changed.

Final initialization timing:

1) It can only be assigned once by final (final int a = 10 )

final int a ;

Initialize before use, assign (assign before constructor) (non-static...)

 


 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324548574&siteId=291194637