methods and arrays

1: Method (Master)
(1) Method: A code block that completes a specific function.
Note: In many languages ​​there are function definitions, while in Java, functions are called methods.
(2) Format:
Modifier return value type method name (parameter type parameter name 1, parameter type parameter name 2...) {
method body statement;
return return value;
}

Modifier: currently use public static. Other modifiers will be explained in detail later.
Return value type: is the data type of the function result.
Method name: It is a name that is convenient for us to call the method.
Parameter type: It is the data type of the
parameter Parameter name: It is the variable
Parameter classification:
Actual parameter: The data that actually participates in the operation
Formal parameter: The variable defined in the method to receive the actual parameter
Method body statement: It is the code block that completes the function
return : End method
Return value: It is the result of the function, which is brought to the caller by return.
(3) Two clarifications:
return value type: the data type of the result
Parameter list: the number of parameters and the corresponding data type
(4) method call
A: method with a clear return value
a: call alone, meaningless
b: output call, which is not very good, because I may need to do further operations without the result. But I usually use it for lectures.
c: Assignment call, recommended scheme
B: Method modified by void type
a: Call alone
(5) Case:
A: Sum scheme
B: Obtain the larger value of two numbers
C: Compare whether the two data are the same
D: Obtain The maximum value of the three numbers
E: Output a star with m rows and n columns
F: Output nn multiplication table
(6) Precautions for the
method A: The method will not be executed if it is not called
B: There is a level relationship between methods, and cannot be nested.
C: When the method is defined, the parameters are used and separated
D: When the method is called, it is not necessary to pass the data type
E: If the method has a clear return value type, there must be a return statement to return.
(7) Method overloading
In the same class, the method name is the same and the parameter list is different. Regardless of the return value.

Different parameter lists:
The number of parameters is different.
The corresponding data types of the parameters are different.
(8) Method overloading case
Comparison of multiple methods with the same name of different types.

2: Array (Master)
(1) Array: A container that stores multiple elements of the same data type.
(2) Features: Each element has a number, starting from 0, and the maximum number is length-1.
The professional name of the number: index
(3) definition format
A: data type [] array name;
B: data type array name [];

It is recommended to use the A method, and forget the B method.
But to be able to understand
(4) the initialization of the array
A: Dynamic initialization
only gives the length, the system gives the default value

Example: int[] arr = new int[3];
B: Static initialization
Give the value, the system determines the length

Example: int[] arr = new int[]{1,2,3};
Simplified version: int[] arr = {1,2,3};
(5) Java memory allocation
A: The stack stores local variables
B: The heap stores all new out
C: Method area (object-oriented part is explained in detail)
D: Local method area (system related)
E: Register (CPU use)

Note:
a: Local variables are defined in the method A variable defined in or on a method declaration.
b: The difference between stack memory and heap memory
Stack: Data disappears after use.
Heap: every new thing has an address
every variable has a default value
byte, short, int, long 0
float, double 0.0
char '\u0000'
boolean false
reference type null
data is used up, the garbage collector is idle time to recycle.
(6) Array memory diagram
A: One array
B: Two arrays
C: Three arrays (two stack variables point to the same heap memory)
(7) Common operations on arrays
A: Traversal
Method 1:
public static void printArray(int [] arr) {
for(int x=0; x<arr.length; x++) {
System.out.println(arr[x]);
}
}

Method 2:
public static void printArray(int[] arr) {
System.out.print("[");
for(int x=0; x<arr.length; x++) {
if(x == arr.length-1) {
System.out.println(arr[x]+"]");
}else {
System.out.println(arr[x]+", ");
}
}
}
B:最值
最大值:
public static int getMax(int[] arr) {
int max = arr[0];

for(int x=1; x<arr.length; x++) {
if(arr[x] > max) {
max = arr[x];
}
}

return max;
}

最小值:
public static int getMin(int[] arr) {
int min = arr[0];

for(int x=1; x<arr.length; x++) {
if(arr[x] < min) {
min = arr[x];
}
}

return min;
}
C:逆序
方式1:
public static void reverse(int[] arr) {
for(int x=0; x<arr.length/2; x++) {
int temp = arr[x];
arr[x] = arr[arr.length-1-x];
arr[arr.length-1-x] = temp;
}
}

方式2:
public static void reverse(int[] arr) {
for(int start=0,end=arr.length-1; start<=end; start++,end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
D:查表
public static String getString(String[] strArray,int index) {
return strArray[index];
}
E:基本查找
方式1:
public static int getIndex(int[] arr,int value) {
for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
return x;
}
}

return -1;
}

方式2:
public static int getIndex(int[] arr,int value) {
int index = -1;

for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
index = x;
break;
}
}

return index;
}

Guess you like

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