Document Version |
Development Tools |
Test Platform |
Project Name |
Date |
author |
Notes |
V1.0 |
|
|
|
2016.02.25 |
Lutianfei |
None |
two-dimensional arrays
Format 1 (dynamic initialization)
Class array2demo{ Public Static void Main(string[] args) {int[] arr =New int[3][2]; System.out.println (arr); System.out.println (arr[0]);the 1th one-dimensional array name of the//arr.System.out.println (arr[1]);the 2nd one-dimensional array name of the//arr.System.out.println (arr[2]);the 3rd one-dimensional array name of the//arr.System.out.println (arr[0][0]); System.out.println (arr[0][1]); }}
Format 2 (dynamic initialization)
- Format:
数据类型[][] 变量名 = new 数据类型[m][];
m
Indicates how many one-dimensional arrays of this two-dimensional array
- This time the number of elements of a one-dimensional array is not given directly, and can be given dynamically.
- Example:
- int[][] arr = new int[3][];
- Arr[0] = new INT[2];
- ARR[1] = new Int[3]
- ARR[2] = new INT[1];
Format 3 (static initialization)
- Format:
数据类型[][] 变量名 = new 数据类型[][]{{元素…},{元素…},{元素…}};
- Simplified version format:
数据类型[][] 变量名 = {{元素…},{元素…},{元素…}};
- Example:
- Int[][] arr = {{1,2,3},{4,6},{6}};
two-dimensional array exercises
- Two-dimensional array traversal
//for Loop ImplementationClass array2test{ Public Static void Main(string[] args) {int[] Arr ={{1,2,3},{4,5,6},{7,8,9}}; for(intx=0; x<arr.length;x++) { for(inty=0; y<arr[x].length;y++) {System.out.print (arr[x][y]+"\ T"); } System.out.println (); } }}//Function ImplementationClass array2test{ Public Static void Main(string[] args) {int[] Arr ={{1,2,3},{4,5,6},{7,8,9}}; PrintArray2 (arr); } Public Static void PrintArray2(int[] arr) { for(intx=0; x<arr.length;x++) { for(inty=0; y<arr[x].length;y++) {System.out.print (arr[x][y]+"\ T"); } System.out.println (); } } }
- two-dimensional array summation
- The data of a company by quarter and month are as follows: unit (million yuan)
- First quarter: 22,66,44
- Second quarter: 77,33,88
- Third quarter: 25,45,65
- Quarter Four: 11,66,99
Class array2test{ Public Static void Main(string[] args) {int[] arr = {{ A, the, -},{ the, -, the},{ -, $, $},{ One, the, About}}; System.out.println (SUM (arr) +"Million"); } Public Static int sum(int[] arr) {intsum =0; for(intx=0; x<arr.length;x++) { for(inty=0; y<arr[x].length;y++) {sum + = Arr[x][y]; } }returnSum }}
- Print Yang Hui triangles (number of lines can be keyboard input)
1: Yang Hui triangle
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 5 10 10 5 1
Analysis:
A: If it is n rows, then the last row is n columns.
B: The first column and the last column of each row are 1 elements.
C: Law:
* The second column of each row begins with the data: the previous column of the previous row + the same column on the previous row
D: The two-dimensional array is composed and then traversed.
When traversing, be careful to imitate the traversal of the 99 multiplication table. Otherwise there will be a lot of 0.
ImportJava.util.scanner;class array2test{ Public Static void Main(string[] args) {Scanner SC =NewScanner (system.in);intn = sc.nextint ();int[] arr =New intN [n]; for(intx=0; x<arr.length;x++) {arr[x][0] =1; ARR[X][X] =1; } for(intx=2; x<arr.length;x++) { for(inty=1; y<=x-1; y++) {Arr[x][y] = arr[x-1][y-1]+arr[x-1][y]; } } for(intx=0; x<arr.length;x++) { for(inty=0; y<=x;y++) {System.out.print (arr[x][y]+"\ T"); } System.out.println (); } }}
- Study questions: 1: Parameter transfer problem
- Numerical pass-through problems in Java
基本类型
: The change of formal parameters has no effect on the actual parameters. ( parameter values are passed)
引用类型
: The change of formal parameters directly affects the actual parameters. (The address value is passed)
Public Static void Main(string[] args) {intA =Ten;intb = -; 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(intAintb) {System.out.println ("A:"-am", B:"+B); A = b; b = A + B; System.out.println ("A:"-am", B:"+B);} Public Static void Change(int[] arr) { for(intx=0; x<arr.length; X + +) {if(arr[x]%2==0) {arr[x]*=2; } }}
- Study Questions 2
- 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 to replace the number,
- Finally, the first and last digits are exchanged. Please arbitrarily give an integer less than 8 bits,
- Then, the encrypted results are printed out in the console.
ImportJava.util.scanner;class jiamidemo{ Public Static void Main(string[] args) {Scanner SC =NewScanner (system.in); System.out.println ("Please input a number:");intNumber = Sc.nextint (); String result = Jiami (number); System.out.println ("Jia mi result is:"+result); } Public StaticStringJiami(intNumber) {int[] arr =New int[8];intindex =0; while(number>0) {Arr[index] = number%Ten; index++; Number/=Ten; } for(intx=0; x<index;x++) {arr[x] + =5; ARR[X]%=Ten; }inttemp = arr[0]; arr[0] = arr[index-1]; arr[index-1] = temp; String s=""; for(intx=0; x<index;x++) {s+=arr[x]; }returnS }}
[========]
The fourth chapter object-oriented
- This chapter highlights
- Object Oriented thinking
- Classes and objects and their use
- Memory diagram of the object
- The difference between a member variable and a local variable
- Anonymous objects
- Package (private)
- this keyword
- Construction method
- Static keyword
Object Oriented thinking
class Elephant {public Span class= "Hljs-keyword" >static void in () {System.out.println ( "loaded into elephant" );}} Class Refrigerator {public static void open () {System.out.println (); public static void close () {System.out.println ( "close the refrigerator Door" );}} Class Demo {public static void main (string[] args) {Refrigerator Call Open door elephant call go into refrigerator call Close }}
how to be more consistent with object-oriented thinking
- A: First analyze which classes
- B: Then analyze what each class should have
- C: The final analysis of the relationship between classes and classes
Object-oriented development, design, features
class
类
: is a set of related properties and behaviors , and is an abstract concept.
对象
: Is the specific manifestation of such things, the specific existence of the individual .
Defining a class is actually a member of the 成员变量
definition 成员方法
class (and)
The same is true in Java for describing things in class.
成员变量
is the property of things.
成员方法
is the act of Things.
How to use
- To create an object:
对象名.成员变量
对象名.成员方法
memory diagram of an object
- Basic initialization procedure for an object
Class phone{//BrandString brand;//Price intPrice//ColorString color;//How to call Public void Pager(String name) {System.out.println ("Call"+name); } Public void SendMessage() {System.out.println ("Send Message!!!"); } Public void PlayGame() {System.out.println ("Play Game!!!"); }}class phonedemo{ Public Static void Main(string[] args) {Phone p =NewPhone (); P.brand ="Nokia"; P.price = -; P.color ="Yellow"; P.call ("chenlixiang~~"); P.sendmessage (); P.playgame (); System.out.println (P.brand +"---"+p.price+"---"+p.color); }}
memory diagram of two objects
- Note: The method has a common problem with memory addresses in the method area.
memory diagram of three objects
- Note: Two references point to the same object
The sixth day of Java Foundation Learning--a two-dimensional array and an object oriented introduction