Preliminary notes on java learning_ three

01 array introduction

  • Introduction: array is a container that can store multiple values of the same data type. It is a continuous memory space opened up in memory
int[] arr = {10,20,'a'};
System.out.println(arr[2]);		// 97

double arr = {10,20,30};
System.out.println(arr[0]);		// 10.0

-----------------------------------------------
    
proposal: The same data type is stored in the array
  • Question: when to use arrays?
    • If there are multiple data to be operated, and multiple data belong to the same group of data, you can consider using array container

02 - definition format of array

1. data type[] Array name;

		int[] arr;

2. Data type array name[];
		
		double arr[];
  • Note: this definition format only defines variables of array type, and the container has not opened up space in memory

03 - static initialization of array

  • Initialization: the process of opening up space for array containers in memory and storing data into space

  • Static initialization:

1. Full format: data type[] Array name = new data type[]{Element 1, Element 2, Element 3...};

		int[] arr = new int[3]{11,22,33};

2. Simplified format: data type[] Array name = {Element 1, Element 2, Element 3...};

		int[] arr = {11,22,33};


be careful: Print array name, See the hexadecimal memory address of the array
    
    	[I@233ac4
         
         
        @ : Separator 
        [ : The current space is an array type, Several brackets, It's a few dimensional array.
        I : Type of container
        233ac4 : Hexadecimal address

04 - element access of array

  • Format: array name [index];
  • Index | subscript | subscript: the number corresponding to the space in the array. The number starts from 0 and increases by + 1 one by one
int[] arr = {11,22,33,44,55};

System.out.pritnln(arr[0]);		// Print

int result = arr[1] + 100;		// calculation

if(arr[2] % 2 == 0){			// judge

}

arr[3] = 66;					// modify

for(int i = 1; i <= arr[4]; i++){	// loop
	System.out.println("itheima");
}

05 - traversal of array

  • Introduction: take out each element in the array
  • Traversal usage scenario: if you want to implement the requirements of the array, you need to traverse the array to operate on [Each] element in the array
for(int i = 0; i < arr.length; i++){
	// i: index
	// arr[i]: element
}

Array name.length : Get the length of the array dynamically (Number of elements)

06 - dynamic initialization of array

  • Introduction: when initializing the array, you only need to manually specify the length, and the system will allocate the default value

  • Format: data type [] array name = new data type [length];

int[] arr = new int[3];

System.out.println(arr[0]);		// 0
  • Classification of default values:
integer: 0
 decimal: 0.0
 Boolean: false
 character: '\u0000'  ---> Unicode character ----> Common white space characters
 Reference data type : null

--------------------------

Reference data type: array, class, Interface

null(Null value) : Can only be assigned to a reference data type
  • Differences between the two initialization methods:

    • Static initialization: specify elements manually, and the system will automatically calculate the length according to the number of elements
    • Dynamic initialization: specify the length manually, and the system will assign the default value
  • There are two usage scenarios for initialization:

    • Static initialization: if the data to be operated is specified in the requirements
    demand: The grade of students in the known class is 100 20 100 30 100
    int[] arr = {100,20,100,30,100};
    
    • Dynamic initialization: I only know how many numbers to save, but I don't know what it is
    demand: Enter five integers on the keyboard, Find the maximum value
     demand: Generate 10 1~100 Random number between, Find the minimum value
    

07 - memory diagram of array

  • Method area: the memory entered when the bytecode file (. class) is loaded
  • Stack: the memory entered when the method is running
  • Heap: the contents of new will enter the heap memory, and usually open up space to generate addresses

The following memory can be consulted by readers themselves

  • Local method stack:
  • Register:

08 array FAQ

  • ArrayIndexOufOfBoundsException: array index out of bounds exception

    • Reason: a nonexistent index is accessed, such as: the circular condition exceeds the length of the array, outputs a negative array subscript or exceeds the subscript value of array length-1
  • NullPointerException: null pointer exception

    • Reason: when the data type variable is referenced and null is recorded, the connection with heap memory will be cut off
    • If you still want to access heap memory data at this time, a null pointer exception will appear
  • Objective: when you see these two errors in the future, you can find the location of the code error and solve the code error according to the error prompt

09 - Introduction to two-dimensional array

  • Introduction: two dimensional array is also a kind of container, which stores one-dimensional array

    • Understanding: a two-dimensional array is actually a nesting of a one-dimensional array
  • Usage scenario of two-dimensional array:

    • There are multiple groups of data to be operated
    • These groups of data are a whole. It is recommended to use a two-dimensional array for maintenance

10 - static initialization of two-dimensional array

  • Static initialization format:
data type[][] Array name = new data type[][]{
		{One dimensional array 1},
		{One dimensional array 2}
};

data type[][] Array name = {
		{One dimensional array 1},
		{One dimensional array 2}
};
---------------------------------------

int[][] arr = {
	{11,22,33},
	{44,55,66}
};
  • Element access of two-dimensional array:
Array name[m Indexes][n Indexes];

m Indexes : Which one-dimensional array do you want to access
n Indexes : Which element of a one-dimensional array do you want to access

System.out.println(arr[1][0]);		// 44

11 - two dimensional array traversal

  • Idea:
    • Traverse the two-dimensional array and get each one-dimensional array
    • Continue to traverse the one-dimensional array to get the specific elements
  • code:
for(int i = 0; i < arr.length; i++){
	// arr[i]: every one-dimensional array
	for(int j = 0; j < arr[i].length; j++){
		System.out.println(arr[i][j]);
	}
}

12 - dynamic initialization of two-dimensional array

  • Format:
data type[][] Array name = new data type[m][n];

m : How many one-dimensional arrays can be stored
n : Each one-dimensional array, How many elements can be stored

int[][] arr = new int[3][1];

A two-dimensional array can store three one-dimensional arrays, Each one-dimensional array can store one element.
  • The two-dimensional array is stored in the memory of the first address of the array. The application is a continuous space starting with the first address of the array. The unit of memory is bytes

13 - method introduction

  • Introduction: method is called function, function and function in other programming languages. In fact, it is the same in essence. It is a code block with independent functions. It will not be executed without calling
  • Benefits:
    • Improve code readability
      • You can classify and manage the crowded and bloated codes according to their functions
      • It can make the code structure clearer and facilitate program maintenance
    • Improve code reusability
      • The code that needs to be written repeatedly is now extracted into the method. You only need to write it once and call the method many times
    • Question: can the method improve the execution efficiency of the program?
    • Answer: no!

14 - method definition and calling format

  • Method common definition format:
public static Return value type method name (parameter list) {
	Method body;
	return Result data;
}

public static : For the time being, Modifier 
return type : It is related to the result data returned by the method.
Method name : See the name and know the meaning, Nomenclature of small hump
 parameter list : Materials required before method operation
 Method body : Method
return :
			1. End method
            2. Returns the result to the caller.
  • Design idea of definition method
1. parameter
	
		problem: In this way, Data to use, Are there flexibility requirements?
				demand: Design a method, Print 10 times HelloWorld --> No
				demand: Design a method, Print n second HelloWorld --> To parameter
				demand: Design a method, Find the maximum of three decimals --> To parameter
				
		problem: If you want parameters, How many? What kind do you want?
				demand: Design a method, Find the maximum of three decimals
						quantity: 3 individual
						type: double
						code: double a, double b, double c
						
				demand: Design a method, Find the minimum value from the array	
						quantity: 1 individual
						type: array
						code: int[] arr

2. Return value

		Step 1: Write code logic first
		Step 2: observation, Is there any way to produce this result
		
					no result: void
		
					public static void print(){
						for(int i = 1; i <= 10; i++){
							System.out.println("HelloWorld");
						}
					}
					
					There are results: adopt return Statement return, And modify the return value type
					
					public static int getMax(int a, int b){
						int c = a > b ? a : b;
						return c;
					}
  • Method call
1. Call with return value

		1). Individual call(Not recommended)
		
				getMax(10,20);

		2). Assignment call(recommend, flexible)
				
				int max = getMax(10,20);
				
				System.out.println(max);
				
				if(max % 2 == 0){
					...
				}
				
				for(int i = 1; i <= max; i++){
					...
				}
				
		3). Output call(Occasionally)
		
				System.out.println(getMax(10,20));
				
2. Without return value

		1) Can only be called individually
		
				print();

15 - frequently asked questions about methods

1. Method does not execute without calling
2. There is a horizontal relationship between methods, Nested definitions are not allowed
3. Method writing order, It has nothing to do with the execution order
4. If the return value type of a method is void, Indicates that this method has no return value, Can be omitted return Statement not written
		If you have to write, Can only be written return; 
5. return Below the statement, Writing code is not allowed, Because it can't be implemented, Is an invalid code.
6. If a method has a specific return value(no void), Must pass return The statement brings back the result data, In any case

		public static int getMax(int a, int b){
			if(a > b){
				return a;
			}else if(b > a){
				return b;
			}
			// Compilation error: the current two returns are controlled by conditions. The compilation time considers that if both conditions are false, this method will not return a value
		}
		
		
		public static int getMax(int a, int b){
			if(a > b){
				return a;
			}else if(b > a){
				return b;
			}else{
				return a;
			}
			
		}

16 method Overload

  • Introduction: a relationship between methods: the same name and different parameters
  • Objective 1: be able to independently identify whether methods are overloaded correctly
In the same class, Same method name, Different parameters, Independent of return value
	Different parameters: 1. Different number 2. Different types 3. Different order
  • Goal 2: be able to understand the benefits of method overloading
There is no need to memorize too many cumbersome methods and names

Assume no method overloading

	printInt(10);
	printString("abc");
	
Method overload:

	println(10);
	println("abc");
	
	According to the actual parameter type, Self matching method.

17 - parameter transfer of method

  • Basic data type of parameter transfer of method: data value is passed

  • Method parameter transfer reference data type: address value is passed

  • Q: what is passed in the parameter passing of the method?

    A: the basic data type passes values, and the reference data type passes addresses

    • Funny: it's a value, and the address value is also a value ☺

My level is limited. If there are errors and deficiencies in the content, readers are welcome to point out and comment.

Keywords: Java JavaSE

Added by mike12255 on Sat, 05 Mar 2022 07:05:22 +0200