The use of methods and arrays in the javaSE series

@[TOC] The use of methods and arrays in the javaSE series

method definition

Methods are similar to "functions" in C language.

Types of methods Methods
here are divided into methods with parameters and methods without parameters . Formal parameters and actual parameters are two entities (this is equivalent to function call by value and call by reference )
1. Non-static methods: ordinary methods/constructs Method
1. Ordinary method: member method/construction method (a special member method)
2. Static method: modified by static
3. Abstract method

The definition of the method is defined by the modifier return value type method name (parameter type formal parameter)
method body code
return;

Notes:
1. Return value type: If the method has a return value, the return value type must be consistent with the returned entity type. If there is no return value, it must be written as void
2. Method name: Named with a small camel case
3. Parameter list: If The method has no parameters, and nothing is written in (). If there are parameters, the parameter type needs to be specified, and multiple parameters are separated by commas. 4.
Method body: the statement to be executed inside the method.
5. In java, the method must be written In the class
6. In java, methods cannot be nested and defined
7. In java, there is no method declaration
8. When a method is defined, the code of the method will not be executed.
9. It will only be executed when it is called. A Method can be called multiple times.

rewriting and overloading

Why do you need to rewrite
When the method functions are the same, but the parameters are different, these methods are rewritten

Overload :

  1. method names must be the same
  2. The parameter lists must be different (the number of parameters is different, the types of parameters are different, and the order of types must be different)
  3. It has nothing to do with whether the return value type is the same//just the method name is the same

insert image description here
Rewrite :

Also known as override. Rewriting is the implementation process of non-static, non-private modification, non-final modification, non-constructive methods, etc. of the parent class by subclasses, and
the return value and formal parameters cannot be changed . That is, the shell remains unchanged, but the core is rewritten!

[Rules for method rewriting]

When the subclass rewrites the method of the parent class, it must generally be consistent with the prototype of the parent class method: the return value type method name (parameter list)
must be completely consistent
, and the return value type of the rewritten method can be different, but it must have a parent-child relationship of.
The access rights cannot be lower than the access rights of the overridden method in the parent class. For example: If the parent class method is modified by public, the method rewritten in the subclass cannot be declared as
protected
. The methods and constructors of the parent class modified by static and private cannot be overridden.

Notice

Avoid calling the overridden method in the construction method:
because when the subclass object is constructed, the superclass construction method will be called first, but in the superclass construction method, there is an overridden method, and dynamic binding occurs at this time. The method of the subclass will be called, but at this time the subclass object itself has not been constructed yet, and the construction is not complete.

Basic concepts of arrays

what is an array

Array: A collection of the same elements, which is a continuous open space in memory (here is similar to (c language syntax, each space has its own number)

Array creation and initialization

Array type [] array name = new array type []
// three ways of writing

  1. int[] arr = new int[]{1,2,3,4,5,6};
  2. int[] arr = new int[10];
  3. int[] arr = {1,3,4,5,6};
  4. //Note that it cannot be written as int[] arr; arr={1,3,4};
  5. int[] arr;
  6. // There is no initialization of the array here, and the default is the corresponding default value of the basic type

How to access the array

system.out.println(arr[0]);

iterate over the array

int[] arr = new arr[]{
    
    1,3,4,5,3};
//方法一
for(int i = 0; i < arr.length ;i++){
    
    
	system.out.println(arr[i]);
}

//方法二
for(数据类型 x : 数组名){
    
    
	system.out.println(x);
}
//这里不能使用下标访问,只能每个元素遍历

Arrays are reference types

// Common reference types are
class class
interface interface
array array
...

What is a reference variable

The essence of a variable is a small memory unit, which stores the value of the variable; and when the variable points to an object, the variable is called a reference. The variable reference is stored on the stack, and the object is stored on the heap.
//The object can be pointed to by multiple references, and a reference can only point to one object, and it is destroyed when the object is not pointed to

insert image description here

null

Null means "null reference" in Java, that is, a reference that does not point to an object.

Array parameter

//Here is similar to the parameter passing in C language
//The contents of the array can be changed by reference, but the reference variable of the object cannot be changed

copy array

int[] arr = {1,3,5,7,85};
int[] newarr = Arrays.copyOf(arr, arr.length);
Copy is divided into deep and shallow copies Here is a brand new array, arr is different from newarr

Arrays.copyOfRange(arr, 0, length)//Here is the subscript of the array (0, length} (in a reasonable range, you can choose at will)

Two-dimensional array

Creation of two-dimensional array

Data type [][] array name = new data type [row number] [column number] { }; //The following is the creation of a two-dimensional array
int[][] arr = new arr[6][3];
int[ ][] arr = new arr[][]{ {34,54},{34,65}}:
int[][] arr = new arr{ {34,65},{64,45}};

A two-dimensional array is actually a special one-dimensional array

insert image description here
arr[i][j] in arr[i] stores the address of a one-dimensional array

Traversal of two-dimensional array

for(int i = 0; i < arr.length; i++){
    
    
	for(int j = 0; j < arr[i].length; j++){
    
    
		System.out.println("arr[i][j]" + " ")
	}
}
for(int[] arr ; array){
    
    
	for(int a ;  arr){
    
    
		System.out.println("a" + " ");
	}
}

Array small exercise

//Bubble Sort

public class Main {
    
    
    public static void sort_buttle(int[] arr){
    
    
        for(int i = 0; i < arr.length-1; i++){
    
    
            for(int j = 0; j < arr.length-1-i; j++){
    
    
                if(arr[j] > arr[j+1]){
    
    
                    int tmp  = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner (System.in);
        int[] arr = {
    
    3,5,6,2,4,1,41};
        sort_buttle(arr);
        for(int a : arr){
    
    
            System.out.println(a);
        }
    }
}

Guess you like

Origin blog.csdn.net/zjy521_/article/details/129484272