java study notes 4: methods, arrays, recursion

A process
1, define: a function to encapsulate, convenience called repeatedly to achieve a particular function block.
2, format:

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2 ....){
             方法体;
			return 返回值;
		}

(1), modifier: currently using static public
(2), Return Value Type: means for defining a return value data type
(3), the method name: a name that is, it exists for convenience we call the method
(4) , parameter type: the data type of parameters passed when calling the method defined in
(5), the parameter name: is a variable, when receiving incoming call method parameter, which actually has a professional term, called the formal parameters, its function is to receive the actual parameter.
(6), Procedure: completion code function
(7), return: returns the specified method, and the method ends value type
(8), the return value: is the result of the function by return back, back to the caller
Note:
① method does not call does
② method is to flat relationships, not nested definitions
defined method ③ when separated by a comma between
call when not in the transmission method ④ data type
⑤ If the method has a clear return value, a value must be brought back by a return

, for example: a method of selecting the maximum value defined in the array

public class ForDemo {
    public static void main(String[] args) {
        int[] arr={1,2,4,5,36,9,6};
        System.out.println(show(arr));
    }
    public static int show(int[] arr){
        int max=arr[0];
        for(int i=1;i<arr.length;i++){
            if(max<arr[i]){
                max=arr[i];
            }
        }
        return (max);
    }
}

3, the method of overload
in the same class, there is a method to allow more than one of the same name, as long as they are different list of parameters (i.e. number of different parameters, the different types of parameters), regardless of the return value.
E.g:

 public static void main(String[] args) {
        
    }
    public static int add(int a,int b){
        
        return 0;
    }

    public static int add(int a, float b) {

        return 0;
    }

    public static int add(int a,int b, float c) {

        return 0;
    }

Second, the array of
the array a plurality of the same data type is a set of storage elements. The equivalent of a container. Array can store the basic data types, can also be stored reference data types.
1, one-dimensional array
format 1: Data type [] array name;
Format 2: Data type array name [];
array initialization: the array must be initialized before use. It is to allocate memory array elements in the array, and assigned to each array element.
Initialization classified: static initialization and dynamic initialization;
①, dynamic initialization:
Format: Data Type [] array name = new data type [array size];
example: int [] ARR = new int [. 5];
②, dynamic initialization:
Format : data type [] = new array name data type [] {element 1, element 2, ...};
example: int [] ARR = new int [] {12,52,21};
2, two-dimensional array
format: ① data type [] [] variable name = new data type [I] [J];
② data type [] [] variable name = new data type [I] [];
③ data type [] [] variable name = {{ element ...}, ... {element} {...}} elements;
problems often arise:
a: an ArrayIndexOutOfBoundsException: array index bounds exception
reasons: access to the index exceeds the length of the array.
b: NullPointerException: null pointer exception
reasons: the array has not a pointer to a heap memory. And you also use to access the elements of an array name.

Third, the recursive
call in a method called recursive method itself
Note : 1 have a recursive export, otherwise death is death recursive recursion can cause a stack overflow
number 2. recursion should not be too much, otherwise there will be a risk of stack overflow
example: ①, factorial 5

 public static void main(String[] args) {
        long num=jieCheng(5);
        System.out.println(num);
    }
  private  static long jieCheng(int i) {
        if(i==1){
           return 1;
        }else{
            return i*jieCheng(--i);
        }

    }

②, rabbit problem (Fibonacci number)
with a pair of rabbits from the first 3 months after birth are born every month one pair of rabbits, bunnies grow up to the third month after the month gave birth to one pair of rabbits, If the rabbit is not dead, and asked for the second ten months of rabbit number is how much?

  分析:
            月份    对数
            1          1
            2          1
            3          2
            4          3
            5          5
            6          8
            7          13
            6          21
           ...          ...

From the result: 1123581321 Fibonacci numbers from the third number of columns, each of the first two numbers is equal to the number of the sum of his
codes:

  public static void main(String[] args){
  int num= rabbitSum(20);
        System.out.println(num);
    }
    private static int rabbitSum(int i) {
        if(i==1||i==2){
           return 1;
        }else{
            return rabbitSum(i-1)+rabbitSum(i-2);
        }
    }
}
Published 24 original articles · won praise 11 · views 2064

Guess you like

Origin blog.csdn.net/weixin_43791069/article/details/84801587