Today is the seventh day I study in Lebyte pull, the teacher is very humorous oh, is I like the drop type, every day to learn times the strength ~~ simply too love, the main content of the teacher today is:

1. Overview and format of two-dimensional arrays

There are many students in each Java foundation class in our school, so we can use arrays to store, and we have many Java foundation classes at the same time. This should also be stored in an array. How do you represent such data? Java provides two-dimensional arrays for us to use so that we can see that a two-dimensional array is actually an array with each element as a one-dimensional array. Data type [][] variable name = new data type [m][n]; Int [][] arr = new int[3][2]; int[] arr = new int[3]; Define a two-dimensional array arR. This two-dimensional array has three one-dimensional arrays named ARR [0], ARR [1],arr[2]. Each one-dimensional array has two elements, Arr [m][n] can be used to obtain the n+1 element of the m+1 one-dimensional array C: Notes A: the following format can also be used to 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]; B is not recommended for either format: note the difference defined below: int x,y; int[] x,y[]; Int [] x,y[]; X =new int[3]; y=new int[3][]; D: case presentation defines two dimensional array, the output of two-dimensional array name, output per name, a one-dimensional array output 123456789101112131415161718192021222324252627282930 two-dimensional array of two elementsCopy the code

Two-dimensional array format 2(Understood)

A: 2d array format 2 Datatype [][] variable name = new datatype [m][]; M is how many one-dimensional arrays there are in this two-dimensional array and instead of giving you the number of elements in the one-dimensional array, you can give it dynamically. Example: int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[3]; arr[2] = new int[1]; B: 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. Output each one-dimensional array of the two-dimensional array, assign the corresponding element value 123456789101112 to the elements of the two-dimensional arrayCopy the code

Two-dimensional array format 3(Understood)

Data type [][] Variable name = new data type [][]{{element... }, {element... }, {element... }... }; Simplified version: Data type [][] Variable name = {{element... }, {element... }, {element... }}; The format belongs to static initialization: specific element value specified by us, by the system to allocate length, for example: int [] [] arr = {{1, 2, 3}, {4 and 6}, {7,8,9}}; Int [] [] arr = {{1, 2, 3}, {5, 6}, {7}}; 12345678910 public static void main(String[] args) {// Static initialization of a two-dimensional array: By assignment to elements, we have a system to calculate the length of the int [] [] arr = new int [] [] {{20, 30}, {1, 3}, {100200300}}; System.out.println(arr.length); System.out.println(arr[2][2]); System.out.println(arr[arr.length-1][arr[arr.length-1].length-1]); / / static initialization of shorthand double [] [] arr2 = {30} {2, {2.14, 3.4}, {1 l, 3.1 F}}; double[] minArr=arr2[0]; System.out.println(minArr[0]); } 123456789101112Copy the code

Exercise 1 Traversal of two-dimensional arrays (Understanding)

public static void main(String[] args) { int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; /* System.out.println(arr[0][0]); System.out.println(arr[0][1]); System.out.println(arr[0][2]); System.out.println(arr[1][0]); System.out.println(arr[1][1]); System.out.println(arr[1][2]); System.out.println(arr[2][0]); System.out.println(arr[2][1]); System.out.println(arr[2][2]); 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]); }}} 12345678910111213141516171819202122Copy the code

Exercise 2: Summing two-dimensional Arrays

public static void main(String[] args) { /* A: The quarterly and monthly statistics of a company are as follows: unit (ten thousand yuan) first quarter: 22,66, 44 second quarter: 77, 33, 88 third quarter: 25, 45, 65 fourth quarter: Int [][] arr = new int[4][3]; 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}; int sum=0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { sum+=arr[i][j]; }} system.out.println (" total: "+sum+"); System.out.println("==================================="); int[][] arr2={{22, 66, 44},{77, 33, 88},{25, 45, 65},{11, 66, 99}}; int sum2 = 0; for (int i = 0; i < arr2.length; i++) { for (int j = 0; j < arr2[i].length; j++) { sum2 += arr2[i][j]; }} system.out. println(" total: "+ sum2 + "); } 1234567891011121314151617181920212223242526272829303132333435 public static void main (String [] args) {/ * B: requirements: Print Yang Hui triangle (the number of lines can be keyboard input) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Look at the diagram analysis rules: 1. 2. Starting from the third row, starting from the second column, the middle number is equal to the sum of the column before the row and the column before it. * */ Scanner sc = new Scanner(system.in); * */ Scanner sc = new Scanner(system.in); System.out.println(" Please enter the number of lines "); int n = sc.nextInt(); Int [][] arr=new int[n][n]; For (int I = 0; i < arr.length; i++) { arr[i][0]=1; arr[i][i]=1; } //2. Starting from the third row, starting from the second column, the middle number = the sum of the previous column in the previous row and this column in the previous row. for (int i =2; i < arr.length; i++) { for (int j =1; j <i; j++) { arr[i][j]=arr[i-1][j-1]+ arr[i - 1][j]; } //3. Print triangle 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(); }} 123456789101112131415161718192021222324252627282930313233343536Copy the code

Two, Java parameter transfer problem and diagram (master)

Public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) { // The reference type is passed as an argument. // The basic type is passed as an argument. Parameter changes do not affect the arguments. // Define two int sides int a = 10; int b = 20; / / the value of output variables System. Out. Println (" a: "+ a +", b: "+ b); //1. 10 20 change(a, b); / / the value of output variables System. Out. Println (" a: "+ a +", b: "+ b); Int [] arr =new int[]{1, 2, 3, 4, 5}; System.out.println(" passed arr"+arr); change(arr); // Outputs the value of the second element in the array system.out.println (arr[1]); //5. 4 } public static void change(int a, int b) { System.out.println("a: " + a + ",b: " + b); //2.a 10 b 20 a = b; //a 20 b = a + b; System.out.println("a: " + a + ",b: " + b); Public static void change(int[] arr) {system.out.println (" received arr:" + arr); for (int x = 0; x < arr.length; x++) { if (arr[x] % 2 == 0) { arr[x] *= 2; }}} 1234567891011121314151617181920212223242526272829303132333435363738Copy the code

Third, the recursion

A: Recursion overview: method definition call method itself phenomenon B: recursion notes to have an exit, otherwise it is dead recursion number can not be too much, otherwise the memory overflow C: recursion example: we learn programming... 12345 public static void main(String[] args) {// Recursion: the phenomenon of calling a method within a method itself. // Recursive need to pay attention to: 1. Recursion should have an exit, otherwise it is dead recursion, dead recursion will cause stack overflow. //2. The number of recursions should not be too much, as there may be stack overflow. //3. Recursion is the idea of splitting and merging. // Recursion in Life: once there was a temple,.... / / to learn Java - looking for a job - to earn money - he married daughter-in-law, Eva - learn Java - looking for a job to earn money - - - to marry daughter-in-law diGUi (50); } private static void diGUi(int num) {//StackOverflowError stack overflow system.out.println (" recursion "); num--; if(num<0){ return; }else{ diGUi(num); }} 12345678910111213141516171819Copy the code

Recursive factorial code implementation (understanding)

Public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) { System.out.println(num); */ int num = 1; for (int i = 5; i >= 1; i--) { num *= i; } System.out.println(num); } 12345678910 public static void main(String[] args) {int r=jieCheng(5); System. The out. Println (" result "+ r); } public static int jieCheng(int i) { if(i==1){ return 1; }else{ return i*jieCheng(i-1); }} 123456789101112Copy the code

(Not death rabbit problem)(Understanding)

Public static void main(String[] args) {public static void main(String[] args) { Demand: Rabbit problem (Fibonacci series) have a pair of rabbits, from the third month after birth, each month gives birth to a pair of rabbits, the rabbit grows to the third month, each month gives birth to a pair of rabbits, if the rabbits die, what is the logarithm of rabbits in the 20th month? 1 1 21 3 2 4 3 5 5 6 8 7 13 8 21 */ / 1 1 2 3 5 8 13 21 34 Fibonacci sequence: starting with the third number, Int [] arr=new int[20]; arr[0]=1; arr[1]=1; For (int I = 2; for (int I = 2; i < arr.length; i++) { arr[i]=arr[i-1]+arr[i-2]; } System.out.println(arr[19]); } 12345678910111213141516171819202122232425262728 public static void main (String [] args) {/ / made a recursion is not a god of death the rabbit int sum=rabbit(20); System.out.println(sum); } private static int rabbit(int i) { if(i==1||i==2){ return 1; }else{ return rabbit(i-1)+rabbit(i-2); }} 123456789101112Copy the code

Fourth, object-oriented

Overview of Object-oriented Thinking (Understanding)

A: Overview of process-oriented thinking Let’s recall the steps we took to complete A requirement these days: first we figured out what we were going to do, then we analyzed how we were going to do it, and finally we coded it. Step by step to achieve, and specific each step we need to achieve and operate. These steps call on each other and collaborate to fulfill our requirements. In each of the above specific steps we are participants, and need to face each specific step and process, which is the most direct embodiment of process oriented. So what is process oriented development? Process oriented development, in fact, is facing the specific each step and process, each step and process to complete, and then call each other by these functional methods, complete the requirements. When the need is single, or simple, we go step by step to operate no problem, and the efficiency is quite high. However, as requirements change and functionality increases, it becomes difficult to face each step. At this time, I began to think about whether these steps and functions could be encapsulated. During encapsulation, different packages could be carried out according to different functions, and similar functions could be encapsulated together. So the structure is a lot clearer. To use it, just find the corresponding class. That’s the idea of object orientation. A: It is a lazy thought that is more in line with our thinking habits. B: It can simplify complex things. C: It changes our roles from executor to commander

Public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) { To call. // We learn programming languages in order to simulate things in the real world and realize informatization. For example: go to the supermarket to buy things billing system, go to the bank to do business system. // In real world, Consists of all things / / things -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - in the corresponding Java class / / attributes -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a member variable members (properties) / / function -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- members method (member functions) / / how to define a class, use the keyword class / / human Person / / so we in the test class, use the attributes and functions in our definition of human. // Class is an abstract concept. To use the properties and methods of a class, you must instantiate the class. We use the keyword new to do this. // We have an object of this class. An object is a concrete representation of a class. With objects, we can use them to invoke properties and functions in a class. // Of course a class can create many objects. Object name =new class name () Person =new Person(); Mingzi =person.name; mingzi=person.name; mingzi=person.name; mingzi=person.name; int nianling= person.age; char ch=person.sex; System.out.println(mingzi); System.out.println(nianling); System.out.println(ch); // The function object name of the calling class. Method name () person.eat(); person.sleep(); person.playGame(); System.out.println("============================"); Person p1 = new Person(); P1. Name = "bill"; p1.age=30; P1. Sex = 'female'; System.out.println(p1.name); System.out.println(p1.age); System.out.println(p1.sex); p1.sleep(); p1.eat(); p1.playGame(); } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950Copy the code

Examples of Object-oriented thinking (Understanding)

A: for example: Wash the clothes. Change dirty clothes – fill water —- soak —— hand rub —- rinse – dry fully automatic washing machine ———– one-button laundry eating C: Object-oriented development is constantly creating objects, using objects, telling objects to do things.

Object Orientation (Overview of Classes and Objects)(Understanding)

A: What do we learn to program for? We learn programming languages in order to simulate things in the real world and realize informatization. For example: go to the supermarket to buy things billing system, go to the bank to do business system. B: How can we describe things in the real world for example: Describe students things name, age, gender.... Study, eat, sleep.... We learn programming languages to simulate things in the real world. The basic unit of the Java language we learn is a class, so we should represent things in a class. Thus we get the real thing and the corresponding relations between classes of things class attribute -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a member variable behavior -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- members method C: define the class is actually define the members of the class members (member variables and methods) a: member variables The variables are defined the same way as before, but in a different position. In a class, outside of a method. B: The member method is the same as before, except that static is removed. D: classes and objects, the concept of a: class: a collection of attributes and behaviors is a group of related b: object: is the embodiment of this kind of things c: for example: monitor of class students object is an object of 1234567891011121314151617181920212223Copy the code

Object Oriented (Definition of Student Class)(Grasp)

A: Student attributes: name, age, address… Behavior: eating, sleeping, attending classes… B: Properties of the correspondence between student affairs and student classes —– member variable behavior —– member methods

Public class Person {// Define a member variable: define a member variable in the class. int age = 23; Char sex = 'male '; Public void sleep() {system.out.println (" sleep "); } public void eat() {system.out.println (" eat "); } public void playGame() {system.out.println (" playGame "); }} 1234567891011121314151617Copy the code

Object Oriented (mobile phone class definition)(Grasp)

Public class Phone {// Define member variable String brand=" xiaomi "; Double price = 799.9; String color=" white "; Public void call(String name){system.out.println (" call "+name+" call "); } public void sendMsg(String name,String content) {system.out.println (" "+ name + ""+content); }} 1234567891011121314Copy the code

Object Oriented (Use of student classes)(Mastery)

A: File name problem Write two classes in A Java file: A basic class and A test class. Suggestion: The file name must be the same as the test class name. B: How do you use it? Use to create objects. C: How do you create objects? Object name = new Class name (); D: How do you use member variables? E: How do I use member methods? Format: object name. Method name (…)

public static void main(String[] args) { Student student = new Student(); // The member variable does not have a value, but has a default value. student.age=18; student.score=60; System.out.println(student.name); System.out.println(student.age); System.out.println(student.score); student.sleep(); student.eat(); System.out.println("==============================="); Student student2 = new Student(); Student2. name = "student2 "; student2.age = 30; student2.score = 50; System.out.println(student2.name); System.out.println(student2.age); System.out.println(student2.score); } 1234567891011121314151617181920Copy the code

Object Oriented (Mobile class use)(Master)

public static void main(String[] args) { Phone phone = new Phone(); String brand = phone.brand; double price = phone.price; String color = phone.color; System.out.println(brand); System.out.println(price); System.out.println(color); // call phle. call(" liu Yifei "); Phone. SendMsg (" Wang Zuxian "," Xiaoqian, this is Ning Caichen "); System.out.println("========================"); Phone phone2 = new Phone(); Phone2. Brand = "meizu"; phone2.price=800; Phone2. Color = "black"; String brand2 = phone2.brand; double price2 = phone2.price; String color2 = phone2.color; System.out.println(brand2); System.out.println(price2); System.out.println(color2); Phone2. Call (" Lin Qingxia "); Phone2. SendMsg (" Guan Zhilin "," thirteen aunts, I am Feihong "); } 1234567891011121314151617181920212223242526Copy the code

Object orientation (an object)(Understood)

public static void main(String[] args) { Student student = new Student(); System.out.println(student); Student. Name =" student "; student.age=50; Student. Score = 3.14; String name=student.name; int age = student.age; double score = student.score; System.out.println(name); System.out.println(age); System.out.println(score); student.sleep(); student.eat(); } 123456789101112131415Copy the code

Object orientation (memory map of two objects)(Understanding)

public static void main(String[] args) { Student student = new Student(); Student. Name =" c "; student.age=18; Student. Score = 80.88; System.out.println(student.name); System.out.println(student.age); System.out.println(student.score); System.out.println("==========================="); Student student2 = new Student(); Student2. Name = "student2 "; student2.age = 38; Student2. Score = 99.99; System.out.println(student2.name); System.out.println(student2.age); System.out.println(student2.score); Println (student); system.out.println (student); system.out.println (student); System.out.println(student2); } 123456789101112131415161718192021Copy the code

Public static void main(String[] args) {Student s1 = new Student(); s1.age = 30; S1. Score = 62.3;

Student s2 = new Student(); S2. Name = "Liu Dehua "; s2.age = 50; S2. Score = 50.5; Student s3 = s1; S1. name = "kwok "; System.out.println(s1.name); // System.out.println(s1.age); // System.out.println(s1.score); // System.out.println(s2.name); // System.out.println(s2.age); // System.out.println(s2.score); // System.out.println(s3.name); // System.out.println(s3.age); // System.out.println(s3.score); // // Displays the address value of the object system.out.println (s1); System.out.println(s2); System.out.println(s3); System.out.println(s1==s2); system.out. println(s1==s2); System.out.println(s1==s3); }Copy the code