The third day of java learning-recursion and arrays

method

formal parameters, actual parameters

return has the function of terminating the program

java is pass by value

Method overloading

Method overloading rules

  1. The method name must be given to him;
  2. The parameter lists must be different (different number, different type, different parameter arrangement order)
  3. The return types of methods can be the same or different
  4. Merely having different return types is not enough to implement method overloading

Command line parameter passing

variable parameter

add(double … num1)

It must be declared as the last parameter, and each method can only have one variable parameter.

recursion

call yourself

Contains two structures

  1. Recursive header: when not to call its own method. If there is no head, it will fall into an infinite loop.
  2. Recursive body: when do you need to call its own method?

When there is too much recursion, it will take up a lot of computer space, reduce the computer's running speed, and affect computer performance.

array

It is a simple data structure, which is a collection of the same data type.

Array declaration and creation

The array must be declared before it can be used

int[]nums (preferred)

Use the new keyword to use it,

nums =new int[5] (the number represents the length of the array)

int nums= new int[5]

Heap and stack

The heap stores all new objects and arrays. It can be shared by all threads and does not store references to other objects.

The stack stores basic variable types (which will include values ​​of this basic type) and variables that reference objects (which will be stored at the specific address of the reference in the heap).

Method area: can be shared by all threads and contains all static and class variables.

The array is initialized like this:

Dynamic initialization: (including default initialization)

int [] b=new int[5] (allocate space first)

b[0]=10; (then assign value)

Static initialization:

int [] a={1,2,3,4,5} (direct assignment)

Default initialization:

The length of the array is determined. Once created, the size cannot be changed. The elements must be of the same type. They can be basic types or reference types. Arrays are also objects because they are created by new.

Use of arrays

for each loop

for (int array : arrays) (arrays.for)
for each 循环,a(数组名).for(自动生成)输出时直接在循环里输出新的数组名

Reverse of array

result[i]=arrays[j];

Two-dimensional array

Definition: Same as one-dimensional array

int[] [] arrays={ {3,4},{4,5},{5,5}};

Print a two-dimensional array

int [][] arrays ={
    
    {
    
    1,2},{
    
    3,4},{
    
    4,5},{
    
    5,6},{
    
    6,7},{
    
    7,8}};
    for(int i=0;i<arrays.length;i++){
    
    
        for(int j=0;j<arrays[i].length;j++){
    
    
            System.out.print(arrays[i][j]+"  ");
        }
        System.out.println();
    }

}

Guess you like

Origin blog.csdn.net/qq_44794782/article/details/115559836