1. Array overview and definition format description
1. Why is there an array
Now you need to make statistics on the salary of employees in a company, such as calculating the average salary, finding the maximum salary, etc. Assuming that the company has 80 employees,
Using the knowledge learned above, the program first needs to declare 80 variables to remember each employee's salary respectively, and then operate, which will be very troublesome.
To solve this problem, Java provides arrays for us to use
Conclusion:
An array is something (container) that stores multiple variables (elements)
The data types of these variables should be consistent
2. Array concept
An array is a collection that stores multiple elements of the same data type. It can also be regarded as a container.
Arrays can store both basic data types and reference data types
3. Array definition format
Format 1: data type [] array name;
Format 2: data type Array name [];
give an example:
int[] a; Defines an array of int type A;
int a[]; Defines an array of int type A;
The first definition is recommended.
2. Array initialization dynamic initialization
1. What is array initialization
Arrays in Java must be initialized before they can be used.
The so-called initialization is to allocate memory space for the array elements in the array and assign values to each array element.
2. Initialized classification:
a: Dynamic initialization: Only the length is specified, and the initialization value is given by the system
b: Static initialization: The initialization value is given, and the length is determined by the system
Note: only one of these two methods can be used, and dynamic and static combination is not allowed
3. Format of dynamic initialization:
Data type [] array name = new data type [array length];
Array length is actually the number of elements in the array.
Example: int[] arr = new int[3]; Defines an array arr of type int, which can store three values of type int.
4. Case demonstration
Output array name and array elements
3. Memory allocation in Java and the difference between stack and heap
In the process of executing Java programs, Java virtual machine will divide the memory it manages into several different data areas
1. Stack: local variables are stored
Local variables: variables in the method definition or on the method declaration are local variables.
2. Heap: all new things are stored
characteristic:
a: Every new thing will be assigned an address value.
b: Each variable has a default value
byte,short,int,long -- 0
float,double -- 0.0
char -- '\u0000'
boolean -- false
Reference data type -- null
c: After use, it becomes garbage, waiting for the garbage collector to recycle it
3. Method area: (explanation of object-oriented part)
4. Local method area: (system related)
5. Register: (cpu usage)
In the process of executing Java programs, Java virtual machine will divide the memory it manages into several different data areas, which have their own characteristics
Some areas exist with the startup of the virtual machine process, while others depend on the startup and destruction of the user thread
End to build and destroy. The memory managed by the Java virtual machine includes the following runtime data areas, as shown in the figure:
4. Memory diagram of array 1 an array
1. Define an array, output the array name and the element value in the array, assign a value to the array, and output the array name and the element value in the array again
5. Memory diagram of array 2 two arrays
1. Define two arrays, output the array name and the element value in the array respectively, assign values to the two arrays respectively, and output the array name and the element value in the array again
public class MyDemo2 { public static void main(String[] args) { int[] arr1 = new int[3]; arr1[0] = 10; arr1[1] = 20; arr1[2] = 30; System.out.println(arr1); //Print the address value of the array System.out.println(arr1[0]); System.out.println(arr1[1]); System.out.println(arr1[2]); System.out.println("======================================="); //Each time new opens up a new space in the heap memory int[] arr2 = new int[3]; System.out.println(arr2); //Print the address value of the array arr2[0] = 100; arr2[1] = 200; arr2[2] = 300; System.out.println(arr2[0]); System.out.println(arr2[1]); System.out.println(arr2[2]); } }
6. Memory diagram of array 3 three references and 2 arrays
1. Define the first array. After definition, assign values to the array elements. After the assignment, the array name and elements are output.
Define the second array. After definition, assign values to the array elements. After the assignment, the array name and elements are output.
Define the third array and assign the address value of the first array to it. (note that the types are consistent), re assign the elements by the name of the third array.
Finally, output the first array name and elements again.
package org.westos.demo3; public class MyTest { public static void main(String[] args) { int[] arr = new int[2]; arr[0] = 25; arr[1] = 30; int[] arr1 = new int[2]; arr1[0] = 96; arr1[1] = 66; int[] arr2 = arr; arr2[0] = 630; arr2[1] = 99; System.out.println(arr[0]); System.out.println(arr[1]); System.out.println("==============================="); System.out.println(arr1[0]); System.out.println(arr1[1]); System.out.println("====================="); System.out.println(arr2[0]); System.out.println(arr2[1]); //Print the address value of the array. System.out.println(arr); System.out.println(arr1); System.out.println(arr2); //Judge whether the address values of two arrays are equal System.out.println(arr == arr1); //false System.out.println(arr == arr2); //true } }
7. Array initialization, static initialization and memory diagram
1. Format of static initialization:
Format: data type [] array name = new data type [] {element 1, element 2,...};
Example: int[] arr = new int[]{1,2,3};
Simplified format:
Data type [] array name = {element 1, element 2,...};
Example: int[] arr = {1,2,3};
2. Drawing demonstration
a: Define an array and output the array name and the element value in the array
package org.westos.demo; public class MyTest2 { public static void main(String[] args) { int[] arr = new int[]{10, 20, 30}; System.out.println(arr); //Print the address value of the array 1b6d3586 arr[0] = 100; System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); } }
1. The length of the array can be obtained dynamically. 2. The index of the last element in the array = array length - 1
package org.westos.demo; public class MyTest3 { public static void main(String[] args) { int[] arr = new int[]{10, 20, 30, 20, 300, 30, 90, 30, 999}; //The array has a length attribute, which can dynamically obtain the length of the array int len = arr.length; System.out.println(len); //Get the value of the last element of the array alt+enter universal error correction key int num = arr[4]; System.out.println(num); //Index of the last element in the array = array length - 1 int index = arr.length - 1; System.out.println(arr[index]); int i = arr[arr.length - 1]; System.out.println(i); } }
8. Two common problems of array operation: out of bounds and null pointer
1. Case demonstration
a:ArrayIndexOutOfBoundsException: array index out of bounds exception
Reason: you accessed a nonexistent index.
b:NullPointerException: null pointer exception
Reason: the array no longer points to heap memory. You also use array names to access elements.
Example of null pointer exception:
package org.westos.demo2; public class MyTest { public static void main(String[] args) { //When you use an array, you will often encounter an exception that the array subscript is out of bounds int[] arr = new int[]{20, 200, 30}; System.out.println(arr); arr = null; //Artificially set null System.out.println(arr.length);//Null pointerexception null pointer exception //When fetching elements, the corner mark is not correct. The array index of ArrayIndexOutOfBoundsException is out of bounds //System.out.println(arr[3]); // arr[4] = 2000; ArrayIndexOutOfBoundsException array index out of bounds exception } }
9. Operation 1 traversal of array
1. Array traversal: output each element in the array in turn.
package org.westos.demo2; public class MyTest2 { public static void main(String[] args) { int[] arr = new int[]{10, 20, 30, 40, 50, 60}; /* System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); System.out.println(arr[3]); System.out.println(arr[4]); System.out.println(arr[5]); */ //Traversal of array for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } System.out.println("====================================="); //Reverse traversal of arrays 60 50 40 30 20 10 for (int i = arr.length - 1; i >= 0; i--) { System.out.println(arr[i]); } } }
10. Operation 2 of the array gets the latest value
A: Get the maximum value of the array (get the maximum or minimum value in the array)
package org.westos.demo2; public class MyTest4 { public static void main(String[] args) { int[] arr = {10, 20, 6000, 300, 50, 15000, 96, 63}; int max = getMax(arr);//alt+enter auto completion System.out.println("The maximum value is:" + max); int[] arr2 = {0, -10, 10, 20, 6000, 300, 50, 15000, 96, 63}; int min = getMin(arr2); System.out.println("The minimum value is:" + min); } public static int getMax(int[] arr) { //Gets the maximum value in the array //Define a referenced element int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } public static int getMin(int[] arr) { //Gets the maximum value in the array //Define a referenced element int min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) { min = arr[i]; } } return min; } }
11. Array operation 3 inversion
1. Array element inversion (i.e. element swapping)
package org.westos.demo3; public class MyTest { public static void main(String[] args) { // A: // Case presentation: // Invert array elements (that is, swap elements) //Idea: first and last elements, exchange values (exchange intermediate variables) and traverse half int[] arr = {10, 20, 30, 40, 50, 60}; //{60,50,40,30,20,10} for (int i = 0; i < arr.length / 2; i++) { int t = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = t; } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
package org.westos.demo3; public class MyTest2 { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90}; //{60,50,40,30,20,10} for (int i = 0, j = arr.length - 1; i < j; i++, j--) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
12. Array operation 4 table lookup method
1. Array table lookup method (enter the index according to the keyboard and find the corresponding week)
import java.util.Scanner; public class MyTest { public static void main(String[] args) { // Case demonstration: array look-up table method (enter the index according to the keyboard and find the corresponding week) find elements according to the index //Define string array String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; Scanner sc = new Scanner(System.in); System.out.println("Please enter index 0--6"); int index = sc.nextInt(); String yuansu = getEle(index, week); System.out.println(yuansu); } public static String getEle(int index, String[] week) { if ( index >= 0 && index <= 6) { String yuansu = week[index]; return yuansu; } else { return "The element you are looking for does not exist. Please check whether your index is entered correctly"; } } }
13. Array operation 5 basic search
1. Array element lookup (find the index of the specified element appearing in the array for the first time)
import java.util.Scanner; public class MyTest2 { public static void main(String[] args) { //Define string array //Index by element String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; Scanner sc = new Scanner(System.in); System.out.println("Please enter week"); // nextLine(); The input is a string String s = sc.nextLine(); int index = getIndexByEle(s, week); System.out.println("The index of this element is:" + index); } /** * @param ele Element to find * @param arr array * @return */ public static int getIndexByEle(String ele, String[] arr) { for (int i = 0; i < arr.length; i++) { //Judge whether the literal contents of two strings are the same. Don't use the = = sign, use the equals() method to judge if (arr[i].equals(ele)) { //Monday = = Monday determines whether the literal content is the same. If it cannot be determined, call the equals() method to determine return i; } } return -1; //-It is a convention that - 1 means not found } }
14. Overview of two-dimensional array and explanation of format 1
1. Overview of 2D array
There are many students in each Java basic class in our school, so we can use arrays to store them, and we have many Java basic classes at the same time.
This should also be stored in an array. How to represent such data? Java provides a two-dimensional array for us to use
This shows that in fact, a two-dimensional array is an array in which each element is a one-dimensional array.
2. Two dimensional array format 1
Data type [] [] variable name = new data type [m][n];
m indicates how many one-dimensional arrays of this two-dimensional array must be written
n represents the number of elements of each one-dimensional array. Optional
give an example:
int[][] arr = new int[3][2];
A two-dimensional array arr is defined
This two-dimensional array has three one-dimensional arrays named arr[0],arr[1],arr[2]
Each one-dimensional array has 2 elements, which can be obtained through arr[m][n]
Represents the N + 1st element of the M + 1st one-dimensional array
3. Precautions
A: The following format can also represent a two-dimensional array
a: Data type array name [] [] = new data type [m][n];
b: Data type [] array name [] = new data type [m][n];
These two formats are not recommended
B: Note the differences defined below
int x,y;
int[] x,y[];
The difference is:
int[] x,y[];// Two arrays are defined, one is a one-dimensional array X and the other is a two-dimensional array y
x=new int[3];
y=new int[3][];
4. Case demonstration
Define two-dimensional array, output two-dimensional array name, output each one-dimensional array name, and output two elements of two-dimensional array
public class MyTest { public static void main(String[] args) { //Two dimensional array: an array whose elements are one-dimensional arrays. The element is a one-dimensional array, so this array is called a two-dimensional array. //Dynamic initialization syntax of two-dimensional array // 2 means that the length of the two-dimensional array is 2 // 3 represents the length of the one-dimensional array in the two-dimensional array, so there is a difference between writing and not writing the parameter 3. int[][] arr = new int[2][3]; //Other ways of writing int arr2[][] = new int[2][3]; int[] arr3[] = new int[2][3]; } }
15. Memory diagram of two-dimensional array format 1
Draw a memory diagram for the above operation
public class MyTest2 { public static void main(String[] args) { //Two dimensional array: an array whose elements are one-dimensional arrays. The element is a one-dimensional array, so this array is called a two-dimensional array. //Dynamic initialization syntax of two-dimensional array // 2 means that the length of the two-dimensional array is 2 // 3 represents the length of the one-dimensional array in the two-dimensional array, so there is a difference between writing and not writing the parameter 3. int[][] arr = new int[2][3]; System.out.println(arr[0]); //It takes the first one-dimensional array in the two-dimensional array System.out.println(arr[1]); //It takes the second one-dimensional array in the two-dimensional array arr[0][0] = 20; arr[0][1] = 30; arr[0][2] = 40; System.out.println(arr[0][0]); System.out.println(arr[0][1]); System.out.println(arr[0][2]); arr[1][0] = 90; arr[1][1] = 100; arr[1][2] = 600; System.out.println(arr[1][0]); System.out.println(arr[1][1]); System.out.println(arr[1][2]); //Reference data types: arrays, classes, and interfaces are all reference data types. To be simple, those that need new are all reference data types. } }
16. Explanation of two-dimensional array format 2 and its memory diagram
1. 2D array format 2
Data type [] [] variable name = new data type [m] [];
m indicates how many one-dimensional arrays this two-dimensional array has
This time, the number of elements of one-dimensional array is not given directly, which can be given dynamically.
give an example:
int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[3];
arr[2] = new int[1];
2. Case demonstration
Define a two-dimensional array, output the name of the two-dimensional array and each one-dimensional array of the two-dimensional array, and then dynamically assign a one-dimensional array to the two-dimensional array
Again, output each one-dimensional array in the two-dimensional array, assign values to the elements in the two-dimensional array, and output the corresponding element values
public class MyTest3 { public static void main(String[] args) { //Two dimensional array: an array whose elements are one-dimensional arrays. The element is a one-dimensional array, so this array is called a two-dimensional array. //Dynamic initialization syntax of two-dimensional array // 2 means that the length of the two-dimensional array is 2 // 3 indicates the length of the one-dimensional array in the two-dimensional array. If it is not specified, it will not help you initialize the one-dimensional array in the two-dimensional array.. int[][] arr = new int[2][]; System.out.println(arr[0]); //It takes the first one-dimensional array in the two-dimensional array System.out.println(arr[1]); //It takes the second one-dimensional array in the two-dimensional array int[] aa = new int[2]; aa[0] = 100; aa[1] = 200; arr[0] = aa; int[] bb = new int[2]; bb[0] = 30; bb[1] = 50; arr[1] = bb; System.out.println(arr[0]); //It takes the first one-dimensional array in the two-dimensional array System.out.println(arr[1]); //It takes the second one-dimensional array in the two-dimensional array arr[0][0] = 20; arr[0][1] = 30; arr[1][0] = 50; arr[1][1] = 60; int a = arr[0][0]; int b = arr[0][1]; int c = arr[1][0]; int d = arr[1][1]; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }
17. Explanation of two-dimensional array format 3 and its memory diagram
1. 2D array format 3
Data type [] [] variable name = new data type [] [] {{element...}, {element...}, {element...}...};
Simplified version:
Data type [] [] variable name = {element...}, {element...}, {element...}};
This format belongs to static initialization: we specify the specific element value and the system allocates the length
give an example:
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
int[][] arr = {{1,2,3},{5,6},{7}};
2. Case demonstration
Define a two-dimensional array, print the one-dimensional array in each two-dimensional array, and print the elements in each two-dimensional array
public class MyTest { public static void main(String[] args) { //Static initialization of two-dimensional array int[][] arr = new int[][]{{20, 30}, {50, 30}, {2, 5, 3, 5, 9}}; //Abbreviation int[][] arr2 = {{20, 30}, {50, 30}, {2, 5, 3}}; int length = arr.length; System.out.println(length); //3 System.out.println(arr[0].length); //2 int length1 = arr[arr.length - 1].length; System.out.println(length1); //5 } }
Illustration of three arrays in two-dimensional array
public class MyTest { public static void main(String[] args) { int[][] arr1 = new int[2][2]; arr1[0][0] = 20; arr1[0][1] = 200; int[][] arr2 = new int[2][2]; arr2[0][0] = 120; arr2[0][1] = 36; int[][] arr3 = arr1; arr3[0][0] = 88; System.out.println(arr1[0][0]); //88 System.out.println(arr1[0][1]); // 200 System.out.println(arr2[0][0]); // 120 System.out.println(arr2[0][1]); // 36 System.out.println(arr3[0][0]); // 88 System.out.println(arr3[0][1]); // 200 } }
18. Two dimensional array exercise 1 traversal
1.
Requirements: 2D array traversal
The outer loop controls the length of the two-dimensional array, which is actually the number of one-dimensional arrays.
The inner loop controls the length of a one-dimensional array.
public class MyTest { public static void main(String[] args) { int[][] arr = new int[][]{{20, 30}, {50, 30, 39, 36}, {2, 5, 3, 5, 9}}; /* System.out.println(arr[0][0]); System.out.println(arr[0][1]); System.out.println(arr[1][0]); System.out.println(arr[1][1]);*/ //Traversal of two-dimensional array for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); for (int j = 0; j < arr[i].length; j++) { System.out.println(arr[i][j]); } } } }
19. Two dimensional array exercise 2 summation
1. Case demonstration
Demand: sum of annual sales of the company
The statistics of a company by quarter and month are as follows: unit (10000 yuan)
First quarter: 22,66,44
Second quarter: 77,33,88
Third quarter: 25,45,65
Fourth quarter: 11,66,99
public class MyTest3 { public static void main(String[] args) { //ctrl+shift + / multiline comment /* A: Case demonstration Demand: sum of annual sales of the company The statistics of a company by quarter and month are as follows: unit (10000 yuan) First quarter: 22, 66, 44 Q2: 77, 33, 88 Q3: 25, 45, 65 Fourth quarter: 11, 66, 99*/ int[][] arr = new int[4][3]; //ctrl + / single line comment // arr[0][0] = 22; // arr[0][1] = 66; // arr[0][2] = 44; arr[0] = new int[]{22, 66, 44}; arr[1] = new int[]{77, 33, 88}; arr[2] = new int[]{25, 45, 65}; arr[3] = new int[]{11, 66, 99}; //Ergodic summation int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { sum = sum + arr[i][j]; } } System.out.println("Total sales are:" + sum + "Ten thousand yuan"); System.out.println("================================"); int[][] arr2 = {{22, 66, 44}, {77, 33, 88}, {25, 45, 65}, {11, 66, 99}}; //int x, y; // Defines two variables of type int, one X and one y //Two arrays are defined, one is one-dimensional array x and the other is two-dimensional array y int[] x, y[]; x = new int[2]; y = new int[2][3]; } }
2. Requirement: print Yang Hui triangle (the number of lines can be entered by keyboard)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Analysis: look at the law of this image
A: The first and last columns of any row are 1
B: Starting from the third row, each data is the sum of the previous column of its previous row and the current column of its previous row.
Steps:
A: First, define a two-dimensional array. If the number of rows is n, we first define the number of columns as n.
The data of this n comes from keyboard entry.
B: Assign 1 to the first and last columns of any row of the two-dimensional array
C: Assign values to other elements according to the law
Starting from the third row, each data is the sum of the previous column of its previous row and the current column of its previous row.
D: Traverse the two-dimensional array.
import java.util.Scanner; public class MyTest { public static void main(String[] args) { /* B: Requirement: print Yang Hui triangle (the number of lines can be entered by keyboard) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 Rule: 1. The beginning and end elements of each line are 1 2. Starting from the third row and starting from the second column, the middle number is equal to the sum of the previous column of the previous row and the current column of the previous row */ Scanner sc = new Scanner(System.in); System.out.println("Please enter the number of rows"); int n = sc.nextInt(); //Define a two-dimensional array so that the number of rows and columns are the same int[][] arr = new int[n][n]; //1. The first and last elements are 1 for (int i = 0; i < arr.length; i++) { arr[i][0] = 1; //arr[i][arr.length - 1] = 1; arr[i][i] = 1; } //Set intermediate element //Starting from the third row and the second column, the number in the middle is equal to the sum of the previous column of the previous row and the current column of the previous row for (int i = 2; i < arr.length; i++) { for (int j = 1; j <= i - 1; j++) { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } } //3. Print triangles for (int i = 0; i < arr.length; i++) { for (int j = 0; j <= i; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } } }
20. Thinking about the problem of parameter transfer in Java and its illustration
Case demonstration
Look at the program write results, and draw a memory diagram to explain
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a: " + a + ",b: " + b);
change(a,b);
System.out.println("a: " + a + ",b: " + b);
int[] arr = {1,2,3,4,5};
change(arr);
System.out.println(arr[1]);
}
public static void change(int a,int b) {
System.out.println("a: " + a + ",b: " + b);
a = b;
b = a + b;
System.out.println("a: " + a + ",b: " + b);
}
public static void change(int[] arr){
for(int x = 0 ; x < arr.length ; x++){
if(arr[x]%2 == 0){
arr[x] *= 2;
}
}
}
recursion
The concept of recursion
1. overview of recursion: the phenomenon of calling method itself in method definition.
2. Precautions
There must be an exit, otherwise it is dead recursion
The number of times should not be too many, otherwise the memory will overflow
3. Recursive example: We learn programming
The idea and illustration of recursive problem solving
Drawing demonstration: the idea and illustration of recursive problem solving (5!)
public class MyTest { public static void main(String[] args) { //Recursion: in java, in the method, it calls the method itself a phenomenon. //Precautions for recursion: // 1. Recursion must have an exit, or it is dead recursion. Dead recursion will overflow the stack. StackOverflowError // 2. The number of recursion should not be too many, which will cause stack overflow. //3. The idea of recursion is the idea of splitting and merging. diGui(10); } public static void diGui(int num) { num--; if (num <= 0) { return; //End method } System.out.println("Method called"); diGui(num); } }
Code implementation and memory diagram of recursive factorial
1. Case demonstration
Requirement: find the factorial of 5
Loop implementation
Recursive implementation
2. Drawing demonstration: Recursive implementation of the factorial of 5 memory graph
Find 5 with a loop!
public class MyTest { public static void main(String[] args) { //Factorial of 5 * 4 * 3 * 2 * 1 int num = 1; for (int i = 5; i >= 1; i--) { num = num * i; } System.out.println(num); } }
Use recursion to find 5!
public class MyTest2 { public static void main(String[] args) { //With the idea of recursion: to solve the factorial of 5 int r = jieCheng(5); System.out.println(r); } public static int jieCheng(int num) { if (num == 1) { return 1; } else { return num * jieCheng(num - 1); } } }
Immortal rabbit problem
1. Case demonstration: Demand: rabbit problem (Fibonacci Series)
A pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what are the logarithm of rabbits in the twentieth month?
It can be seen that the data of rabbit object is: 1, 1, 2, 3, 5, 8
public class MyTest { public static void main(String[] args) { int r = num(20); System.out.println(r); } public static int num(int m){ if(m==1||m==2){ return 1; }else { return num(m-1)+num(m-2); } } }