JAVA_DAY05 (based on Java, methods and arrays)

Source: Internet
Author: User

1: Method (Master)
(1) Method: A block of code that completes a specific function.
Note: In many languages there is a definition of a function, whereas in Java, a function is called a method.
(2) Format:
Modifier returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...) {
Method body Statement;
return value;
}

Modifier: Use public static now. Additional modifiers are explained in more detail later
Return value type: is the data type of the functional result
Method Name: It is a name that makes it easy for us to call this method.
Parameter type: is the data type of the parameter
Parameter name: is the variable
Parameter classification:
Arguments (actual parameters): data that actually participates in the operation
Formal parameter (formal argument): A variable defined on a method that receives an actual argument
Method Body Statement: Is the code block that completes the function
Return: End method
Return value: Is the result of the function, which is brought to the caller by return.
(3) Two clear:
Return value type: The data type of the result
Parameter list: number of parameters and corresponding data types
(4) Method invocation
A: A method with a definite return value
A: Call alone, no meaning
B: The output call is not good, because I may need to do further operations without results. But I used the lecture in general.
C: Assignment invocation, recommended scenario
Methods of b:void Type modification
A: Call alone
(5) Case:
A: Summation scheme
B: Get a larger value from two numbers
C: Compare whether two data is the same
D: Get the maximum value in three numbers
E: Star of output m row n column
F: output nn multiplication table
(6) Precautions for the method
A: Method does not call do not execute
B: The method is a peer relationship and cannot be nested defined
C: When the method is defined, the arguments are separated by the
D: The method does not pass the data type at the time of the call
E: If the method has an explicit return value type, a return statement must be returned.
(7) Method overloading
In the same class, the method name is the same, and the parameter list is different. Is independent of the return value.

The parameter list is different:
The number of parameters is different.
The corresponding data type of the parameter is different.
(8) Method overloading case
A comparison of multiple methods with the same name for 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, the maximum number is the length-1.
Number of professional names: Index
(3) Define the format
A: Data type [] array name;
B: Data type array name [];

Recommendation is a way, B method forget it.
But to be able to read
(4) Initialization of arrays
A: Dynamic Initialization
Give only the length, the system gives the default value

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

Example: int[] arr = new int[]{1,2,3};
Simplified version: int[] arr = {£ º};
(5) memory allocation in Java
A: Stack stores local variables
B: Heap Storage for all new
C: Method Area (object-oriented part explained in detail)
D: Local Method Area (System-dependent)
E: Register (CPU use)

Attention:
A: A local variable is defined in the method definition or on a method declaration.
B: The difference between stack memory and heap memory
Stack: When the data is used, it disappears.
Heap: Every new one comes with an address.
Each variable has a default value
Byte,short,int,long 0
float,double 0.0
Char ' \u0000 '
Boolean false
Reference type NULL
After the data is used, it is recycled when the garbage collector is idle.
(6) Array Memory graph
A: An array
B: Two arrays
C: Three arrays (two stack variables point to the same heap memory)
(7) Common operations for arrays
A: Traverse
Mode 1:
public static void PrintArray (int[] arr) {
for (int x=0; x<arr.length; x + +) {
System.out.println (Arr[x]);
}
}

Mode 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: Best Value
Maximum value:
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;
}

Minimum value:
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: Reverse Order
Mode 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;
}
}

Mode 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: Check Table
public static String getString (string[] Strarray,int index) {
return Strarray[index];
}
E: Basic Search
Mode 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;
}

Mode 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;
}

JAVA_DAY05 (based on Java, methods and arrays)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.