2D Arrays and Object Oriented Basics

1: Two-dimensional array (understanding)
(1) The element is an array of one-dimensional arrays.
(2) Format:

  1. datatype[][] arrayname = new datatype[m][n];
  2. datatype[][] arrayname = new datatype[m][];
  3. datatype[][] arrayname = new datatype[][]{{...},{...},{...}};
  4. datatype[][] arrayname = {{...},{...},{...}};

(3) Case (Master):

  1. Traversing a two-dimensional array
  2. Summation of 2D Arrays
  3. Yang Hui triangle

Yang Hui Triangle:

public class Array2Demo {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[n][n];
        /*
         * Ideas:
         * 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;
         * C: First define a two-dimensional array, if the number of rows is n, the number of columns is also n;
         * D: Assign 1 to the first and last columns of any row of this two-dimensional array;
         * E: Assign values ​​to other elements according to the rules, starting from the third row, each data is the previous column of its previous row
         * and the sum of this column on the row above it.
         * F: Traverse this two-dimensional array.
         */
        for(int i=0;i<arr.length;i++){
            arr[i][0] = 1;
            arr[i][i] = 1;
        }
        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];
            }
        }
        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();
        }
    }
}

 

2: Two thinking questions (understanding)
(1) Parameter passing
in Java There is only value passing in Java.

Basic type: The change of the formal parameter does not affect the actual parameter
Reference type: The change of the formal parameter directly affects the actual parameter
(2) Data encryption problem
A small comprehensive case.

/*
A company uses a public telephone to transmit data information, the data is an integer less than 8 digits, in order to ensure security,
Encryption is required during transmission, and the encryption rules are as follows:
    First reverse the data, then add 5 to each digit, and replace the digit with the remainder of dividing the sum by 10,
    Finally swap the first and last digits. Please arbitrarily give an integer less than 8 digits,
    Then, print the encrypted result on the console.
    
Topic requirements:
    A: The data is an integer less than 8 bits
        Define a data of type int
        int number = 123456;
    B: Encryption Rules
        a: Reverse the data first
            result 654321
        b: Then add 5 to each digit and replace the digit with the remainder after dividing the sum by 10
            result 109876
        c: Swap the first and last digits at the end
            result609871
    C: Output the encrypted result to the console
*/
public class JMDemo {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println( "Please enter a data: " );
         int number = sc.nextInt();
        String result = JM(number);
        System.out.println( "After encryption: "+ result);
            
    }
    public static String JM(int number){
        int[] arr = new int[8];
        int index = 0;
        while(number > 0){
            arr[index] = number%10;
            index++;
            number/=10;
        }
        for(int i=0;i<index;i++){
            arr[i] = (arr[i]+5)%10;
        }
        int temp;
        temp = arr[0];
         arr[0] = arr[index-1];
        arr[index-1] = temp;
        
        String s = "";
        for(int i=0;i<index;i++){
            s += arr[i];
        }
        return s;
    }
}

3: Object-oriented (mastering)
(1) Object-oriented Object
-oriented is a process-oriented programming idea
(2) Object-oriented thinking characteristics

  • A: It is a kind of thinking that is more in line with our thinking habits
  • B: Simplify complex things
  • C: Let's go from executor to conductor

For example:
buy a computer, do
laundry ,
cook
...
everything is an object
(3) put the elephant in the refrigerator (understand)

  • A: Process-oriented implementation
  • B: Object-Oriented Implementation


Note: How to make our operations more in line with object-oriented thinking?

  • A: What are the categories
  • B: What are the members of each class
  • C: class and class relationship

(4) Classes and objects
A: things in the real world,
basic descriptions of attribute things
, functions of behavior things
B: The most basic unit in the Java language is a class. Therefore, we use classes to embody things.
C: Classes,
Member Variables, Things, Attributes
, Member Methods, Things, Behaviors,
D: Classes: It is a collection of related attributes and behaviors. is an abstract concept.
Object: It is the concrete existence of this kind of thing, and it is a concrete instance. (Object)

Example:
Student: Class
Monitor: Object
(5) Class Definition and Use
A: Class Definition The
format of the member variable definition is the same as before, but the location is different, in the class, outside the method.
The member method definition format is the same as before, that is, the static is removed.
B: Use the content of the class
a: Create an object? Format
class name object name = new class name ();
b: How to use member variables and member methods
Object name. Member variable
Object name. Member method ()
(6) Case:
A: Definition and use of student class
B: Definition and use of mobile phone class
(7) Memory map
A: Memory map of an object

B: Memory map of two objects

C: Memory map of three objects

 


(8) Development, design and characteristics of Java programs

  • A: Development: It is to continuously create objects and call functions through objects
  • B: Design: is to manage and maintain the relationship between objects
  • C: Features
  1. package
  2. inherit
  3. polymorphism

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325381972&siteId=291194637