06.01 Overview of two-dimensional arrays and explanation of format 1
Two-dimensional array Overview: A two-dimensional array is actually an array of elements as one-dimensional arrays
Format 1: Data type [] Variable name = new data type [m][n];
M indicates how many one-dimensional arrays are in this two-dimensional array, and N represents the number of elements per one-dimensional array
Example: int[][] arr = new int[3][2];
Defines a two-dimensional array, arr
This two-dimensional array has 3 one-dimensional arrays, the name is arr[0],arr[1],arr[2]
Each one-dimensional array has 2 elements, which can be obtained by arr[m][n], representing the first n+1 elements of the m+1 one-dimensional array
Format 1 can also be written as int arr[][]; Int[] arr[]; But it's not recommended.
Attention:
int x, y; Represents a variable x, y, which defines two int types at the same time
Int[] x,y[]; Represents a one-dimensional array x, a two-dimensional array y
Cases:
1//define a two-dimensional array with 3 one-dimensional arrays, each 2//one-dimensional array having 2 elements 3 int[][] arr = new int[3][2]; 4 5//Output two-dimensional array name 6 System.out.println (arr);//[[[email protected] 7 8//output a two-dimensional array of 1th elements the name of a one-dimensional array 9 System.out.println ( Arr[0]); [Email protected]10 11//Output two-dimensional array elements of SYSTEM.OUT.PRINTLN (arr[0][0]);//0
06.02 memory plots for two-dimensional array format 1
06.03 explanation of two-dimensional array format 2
Format 2: Data type [] Variable name = new data type [m][];
M indicates how many one-dimensional arrays are in this two-dimensional array, but does not directly give the number of elements of a one-dimensional array, which can be dynamically given
Cases:
1 int[][] arr = new int[3][]; Define a two-dimensional array with 3 one-dimensional arrays 2 arr[0] = new INT[2]; The 1th one-dimensional array has 2 elements of 3 arr[1] = new INT[3]; The 2nd one-dimensional array has 3 elements of 4 arr[2] = new INT[1]; 3rd one-dimensional array has 1 elements
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 int[][] arr = new int[3][]; 6 7 System . OUT.PRINTLN (arr); [[[Email protected] 8 System.out.println (arr[0]);//null 9 //dynamically allocate space for each one-dimensional array arr[0] = new int[2];12 arr[1] = new int[3];13 arr[2] = new int[1];14 System.out.println (arr[0]);//[[email protected]16 System.out.println (arr[0][0]);//017 arr[1][0] = 100;19 arr[2][0] = 200;20 }21}
06.04 memory plots for two-dimensional array format 2
06.05 explanation of two-dimensional array format 3
Format 3: Data type [] Variable name = new data type [][]{{element ...},{element ...},{element ...}};
Simplified version format: data type [] Variable name = {{element ...},{element ...},{element ...}};
Example: int[][] arr = {{1,2,3},{4,5},{6}};
06.06 memory plots for two-dimensional array format 3
06.07 Two-dimensional array Exercise 1 times Calendar
Two-dimensional array traversal
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 int[][] arr = {{1,2,3},{4,5,6},{7,8,9}}; 6
//Outer loop control is the length of the two-dimensional array, in fact, the number of one-dimensional array 7 for (int i = 0;i < arr.length;i++) 8 {9 //inner loop control of the length of the one-dimensional array for ( Int J = 0;j < arr[i].length;j++) One { System.out.print (arr[i][j]+ ""); }14 System.out.println (); }16 }17}
06.08 two-dimensional array Exercise 2 summation
The data of a company by quarter and month are as follows: unit (million yuan)
First quarter: 22,67,43
Second quarter: 77,33,88
Third quarter: 25,43,65
Quarter Four: 11,66,99
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 //The data of the topic is represented by two-dimensional data 6 int[][] arr = {{22,67,43 },{77,33,88},{25,43,65},{11,66,99}}; 7 8 //define a summation variable 9 int sum = 0;10 //Traverse two-dimensional array one for (int i = 0;i < arr.length;i++) {13 System.out.print ("First" + (I+1) + "Quarterly:"); + for (int j = 0;j < arr[i].length;j++) System.out.print (arr[i][j]+ "million"); sum + + arr[i][j];18 }19 System.out.println (); }21 System.out.println ("Sum:" +sum+ "million"); }23}
Operation Result:
The 1th Quarter is: 22万67万43万 2nd quarter is: 77万33万88万 3rd quarter respectively: 25万43万65万 4th quarter is: 11万66万99万 sum is: 6.39 million yuan
06.09 two-dimensional array Exercise 3 Yang Hui Triangle
Print Yang Hui triangles (number of lines can be keyboard input)
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 class Demo 2 {3 public static void main (string[] args) 4 {5 Scanner sc = new Scanner (system.in); 6 System.out.println ("Please enter a number:"); 7 int n = sc.nextint (); 8 9//Create a two-dimensional array based on the data entered by the keyboard int[][] arr = new INT[N][N];11 12//assigns a value of 113 F to the first and last column of any row of a two-dimensional array or (int i = 0;i < arr.length;i++) {arr[i][0] = 1;//any row first column arr[i][i] = 1;//any row Last column}18 for (int i = 2;i < arr.length;i++) (int j = 1;j <= i-1; j+ +) 22 {23//each data is the previous column of its previous row and the sum of the columns of its previous row arr[i][j] = Arr[i-1][j-1] + arr[i-1][j]; 25}26}27 28//Traverse two-dimensional array (int i = 0;i < arr.length;i++) 30 {31 for (int j = 0;j <= i;j++), {System.out.print (arr[i][j]+ "\ T"), 34}35 System.out.println ();}37}38}
06.10 parameter transfer problem in study questions 1Java
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 int a = ten; 6 int b = +; 7 System.out . println ("A:" +a+ ", B:" +b); A:10,b:20 8 Change (b), 9 System.out.println ("A:" +a+ ", B:" +b "); a:10,b:2010 int[] arr = {1,2,3,4,5};12 change (arr); System.out.println (arr[1]);//414 } The public static void change (int a,int b) is {System.out.println ("A:" +a+ ", B:" +b); a:10,b:2018 a = b;19 B = A + b;20 System.out.println ("A:" +a+ ", B:" +b "); a:20,b:4021 }22, static void Change (int[] arr), {for (int x=0; x<arr.length; x + +) (arr[x]%2==0) { arr[x]*=2;30 }31 }32 }33}
Parameter passing problems in Java:
Basic type: Changes in formal parameters do not affect actual parameters
Reference type: Changes in formal parameters directly affect actual parameters
Analysis of 06.11 Study Questions 2 encryption problem
Data encryption issues
A company uses a public telephone to pass data information, which is an integer less than 8 bits, in order to ensure security, encryption is required during delivery, and the encryption rules are as follows:
First the data is reversed, then each digit is added 5, and then divided by the remainder of 10 instead of the number, and finally the first and last digit exchange. Give an integer that is less than 8 bits, and then print the encrypted result in the console.
such as input 123456 output 609871
Analysis:
1. The data is an integer less than 8 bits and can be defined by an int type of data
2. Encryption rules
A: First, reverse the data
B: Then add 5 to each digit, then replace the number by the remainder of 10.
C: Finally, the first and last digits are exchanged
3. Output the encrypted results in the console
Through analysis, data needs to be transformed into arrays
1. Define a data
2. Define an array, int[] arr = new Int[8]; No more than 8.
When assigning a value, record the change of the index with a variable.
3. Get every single data
int GE = number%10
int Shi = number/10%10
int Bai = number/100%10
06.12 Study Questions 2 implementation of code for cryptographic problems
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 //define one data 6 int num = 123456; 7 //define a number Group 8 int[] arr = new Int[8]; 9 //Gets data for each bit of data stored in an array of int index = 0;11 while (num > 0) { arr [index] = num% 10;14 index++;15 num/= 10;16 }17 for (int i = 0;i < index; i++) Arr[i] + = 5;21 arr[i]%= 10;22 }23 int temp = arr[0];25 arr[0] = arr[index-1];26 Arr[index- 1] = temp;27 for (int i = 0;i < index;i++) { System.out.print (arr[i]+ ""); }32 System.out.println (); }34}
06.13 Study Questions 2 code improvements for encryption issues
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 Scanner sc = new Scanner (system.in); 6 Sys Tem.out.print ("raw Data:"); 7 int num = Sc.nextint (); 8 //define an array 9 int[] arr = new int[8];10 //Get data from each bit of data stored in the array int index = 0;1 2 while (num > 0) @ arr[index] = num% 10;15 index++;16 num/= 10;17 }18 (int i = 0;i < index; i++) { Arr[i] + = 5;22 arr[i]%= 10;23 }24 int temp = Arr[0];26
arr[0] = arr[index-1];27 arr[index-1] = temp;28 System.out.print ("Encrypted data:"); (int i = 0;i < index;i++) { System.out.print (arr[i]); }34 System.out.println (); }36}
06.14 Introduction to ideas and overview of process-oriented thinking
We know from the previous explanation that the class can be stored in a method, so we consider using classes to encapsulate these methods, and in the future do the array operations, do not have to find specific methods, first find the class, and then use the method in this class. This is how object-oriented thinking is programmed.
Process-oriented development, in fact, is facing the specific steps and processes, each step and process is completed, and then by these functional methods call each other to complete the requirements.
Process-oriented representative language: C language
06.15 overview of Object-oriented thinking
An overview of object-oriented thinking: Object-oriented is a process-oriented programming idea
Process oriented: For process-oriented thinking, emphasis is on process (action). For example, C language is a process-oriented language
Object-oriented: for object-oriented thinking, the emphasis is on objects (entities). For example, C + +, Java, C # are object-oriented languages
Object-oriented thinking features:
1. Object-oriented is a common thought, in line with people's thinking habits
2. Object-oriented presence, simplifying complex issues
3. Object-oriented appearance, so that once in the process of the executor, become the object of the leader
06.16 elephant put into refrigerator case
Put the elephant in the refrigerator
Process oriented: Action
1: Open the refrigerator door
2: Loaded into the elephant
3: Close the refrigerator door
Object-oriented: When using object-oriented thought to embody, give a three-sentence rule, let us conform to the object-oriented thought
1. What are the classes
2. What does each class have?
3. What is the direct relationship between classes and classes
The analysis of the elephant in the refrigerator (how to analyze what kind?) Uml. Noun extraction method. )
1. What are the categories: elephants, refrigerators, demos
2. What each class has: [Elephant: Go in], [refrigerator: Door closed], [Demo:main Method]
3. Class-To-class direct relationship: Use the functions of elephants and refrigerators in demo
06.17 Development, design and features
Object-oriented development: it is constantly creating objects, using objects, directing objects to do things.
Object-oriented design: is the relationship between managing and maintaining objects.
Object-oriented features:
Package (encapsulation)
Inheritance (inheritance)
Polymorphic (polymorphism)
06.18 Classes and objects
How to represent a real world thing:
A property is a descriptive message of that thing.
Behavior is what the thing can do
The basic unit of the Java language we learn is the class, so we should put things in a class to embody
Property and behavior of a thing corresponding member variable and member method in class
A class is a set of related properties and behaviors
Objects are the concrete embodiment of such things
Class is abstract, conceptual, representing a class of things, such as human, cat
The object is concrete, practical, and represents a specific thing.
A class is a description of a thing, an instance of that object, created in Java through new
Overview of definitions for class 06.19
Diagram of class to object:
The drawings are classes, and every car is an object.
Things in the real world
Attribute person's height, weight, etc.
The perpetrator can learn, eat, etc.
The same is true in Java for describing things in class.
A member variable is a property of a thing
A member method is the act of something.
Defining a class is actually a member of the definition class (member variables and member methods)
06.20 definition of the student class
How to define student classes
Follow the process of thing to class step by step analysis
Student things:
Properties: Name, age, Address ...
Behavior: study, eat, sleep ...
To convert a thing to a corresponding class:
Student class:
Member variables: Name, age, Address ...
Member methods: Learn, eat, sleep ...
1 class Student 2 {3 //Definition Variable 4 //Name 5 string name; 6 //age 7 int ages; 8 //Address 9 String addr ESS;10 //Definition Method// method of study//learning methods Public Void study () { System.out.println ("Students Love Learning") }17 //meal method, public void Eat () 22 ("Learning hungry, to eat") }23 //Sleeping method of public void Sleep () () ("Learning tired, to Sleep"); 28 }29}
06.21 definition of mobile phone class
Phone things:
Properties: Brand, price, color ...
Behavior: Call, send text, play games ...
Mobile Phone class:
Member variables: brand, price, color
Member method: Call, send text, play games
1 class Phone 2 {3// brand 4 string brand; 5// price 6 int prices; 7 //color 8 String color; 9 10 //Telephone method one-to-one public void-call (String name) { System.out.println ("to" +name+ "); page//Text message method of public void SendMessage () { System.out.println ("Mass sms"); }21 The method of playing the game is the public void PlayGame () System.out.println ("Play the Game"); }27}
06.22 use of student classes
Create object: Class Name Object name = new class name ();
Use member Variable: Object name. Member Variable
Use member method: Object name. Member method
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 Student s = new Student (); 6 SYSTEM.OUT.PR Intln (s.name+ "..." +s.age+ ".." +s.address); 7 //Assign value to member variable 8 s.name = "Xiaoming"; 9 s.age = 25;10 s.address = "Shanghai"; System.out.println (s.name+ "...") +s.age+ ".." +s.address);/ /Call Method S.study (); }16}17 class Student +/ /definition variable //name String name;23 //age int age;25 //Address string address;27// definition method // Method of study (study) ( System.out.println) ("Students love to Learn"); }34 //meal Method 36 Public void Eat () Notoginseng { SYSTEM.OUT.PRINTLN ("study hungry, Want to eat"); }40 //Sleeping method for the public Void Sleep () System.out.println ("Learning tired, to Sleep"); }46}
Operation Result:
Null.. 0..null Xiao Ming. 25. Shanghai Students Love Learning
06.23 use of mobile phone class
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 phone p = new Phone (); 6 System.out.printl N (p.brand+ "..." +p.price+ "..." +p.color); 7 //Assign value to member variable 8 p.brand = "Nokia"; 9 p.price = 450;10 P.color = "BLACK"; System.out.println " ... "+p.price+" ... "+p.color);/ /Call member Method P.call (" xiaoming "); }16}17 class Phone + //Brand 21< C13/>string brand;22// price int price;24 //color String color;26 //Phone method public void Call (String name) { System.out.println ("to" +name+ "); }32 // Text message method of public void SendMessage () () System.out.println ("Mass sms"); Notoginseng }38 Ways to play the game PlayGame () () () ( "play the Game") (SYSTEM.OUT.PRINTLN); }44}
Operation Result:
Null... 0...null Nokia ... 450 ... black give Xiao Ming a call
06.24 memory diagram of an object
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 phone p = new Phone (); 6 System.out.printl N (p.brand+ "..." +p.price+ "..." +p.color); 7 8 p.brand = "Nokia"; 9 p.price = 450;10 P.color = "BLACK"; System.out.println (p.brand+ "..." + P.price+ "..." +p.color), p.call ("Xiaoming"), }15}16 class Phone, {+ String brand;20 int Price;21 string color;22 public void Call (string name) System.out.println ("Give" + name+ "call"); }27}
06.25 memory graphs for two objects
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 phone p1 = new Phone (); 6 p1.brand = "Nokia" ; 7 p1.price = 8 P1.color = "BLACK"; 9 System.out.println (p1.brand+ "..." +p1.price+ "..." +p1.color); 10 p1.call ("Xiao Ming"); Phone P2 = new phone (), P2.brand = "Millet", p2.price = 1699;15 p2.color = "white"; System.out.println (p2.brand+ "..." +p2.price+ "..." +p2.color); P2.call ("Wang Choi"); }20}21 class Phone $ brand;25 int price;26 string color;27 public void Call (string name) { SYSTEM.OUT.PRINTLN ("Call" +name+ "); }32}
06.26 memory graphs for three objects
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 phone p1 = new Phone (); 6 p1.brand = "Nokia" ; 7 p1.price = 8 P1.color = "Black", 9 System.out.println (p1.brand+ "..." +p1.price+ "..." +p1.color); 10 11 phone P2 = new phone (), P2.brand = "Millet", p2.price = 1699;14 p2.color = "white"; System.out.println (p2.brand+ "..." +p2.price+ "..." +p2.color); Phone p3 = p1;18 System.out.println ( P3.brand+ "..." +p3.price+ "..." +p3.color); P3.brand = "Meizu"; p3.price = 2999;21 p3.color = "Gold"; 22 System.out.println (p1.brand+ "..." +p1.price+ "..." +p1.color); }25-Class Phone , {brand;29 int price;30 string color;31 -public void call (St Ring name) { System.out.println ("Call" +name+ "); }36}
Javase Learning Summary No. 06 Day _java Language Fundamentals 2 & Object oriented 1