Javase Course Chapter fourth the methods, arrays, Java new features

Source: Internet
Author: User
Tags array sort

Role:
1, can understand the method declaration and use
2, can understand the array of reference passing
3, can understand the new Java features array support


Declaration and use of a method
1 Definition of the method
A method is a block of code that can be called repeatedly.


2 definition format of the method
A public static return value type method name (type parameter 1, type parameter 2 ...) {
Method Body:
Program statements
[Return expression;]
}
b The method defined in this section, because it can directly use the main method call, so add the method declaration public static Two keywords
C Note
1/If void is written on the return value type, then this method does not return a value, it cannot use return to return content
2/Method Naming specification: First letter lowercase, followed by the first letter of each word capitalized.


3 Method overloading
Definition: The method overload is the same method name, but the parameters of the type and the number of parameters, by passing the number of parameters and the type of different to complete the different functions of the method call.
Tip: The System.out.println () method also belongs to overloaded methods
Note: Method overloads must be judged based on the type or number of arguments.
************************************
public int addfun (int a,int b) {
return a+b;
}


public int addfun (int a,int b,int c) {
return a+b+c;
}
************************************


4 ending a method with return
Return returns the End method, in addition to the result of returning a method, to return content.


Recursive invocation of 5 methods
Definition: Recursive invocation is a special form of invocation, which is the method of invoking itself.
Note: In the recursive operation, be sure to write out the method of the sleepover judgment, otherwise there will be a memory overflow bug.
************************************
1+. +100 Recursive calls
public static int Fun (int temp) {
if (temp==1) {
return 1;
}else{
Return Temp+fun (temp-1);
}
}
************************************


Reference passing of two arrays
1 Receiving and returning arrays
Definition: A method can receive an array and can return an array. If the method receives an array, the modifications made to the array are preserved all [because the arrays use memory, so the modifications are preserved].
************************************
Modify the array, and the array changes are saved
public static void Updatearr (int[] array) {
array[0]=12;
}


public static void Main (string[] args) {


Int[] Array = {n/a};
Updatearr (array);
System.out.println (Array[0]);

}
************************************


Note: In addition to being able to receive arrays, the method can return an array by method, simply by returning an array on the return value type, at an explicit declaration.


2 example explanation, array ordering (tells the Java API's power)
A array sort Reference method = = Develop yourself (can use bubble sort)
Bubble sort from small to large
************************************
public static void Mpsorts (int[] arrsys) {//{10,9,8,7}
int temp=0;
for (int i = 0; i < arrsys.length; i++) {
I=0, 10 need to compare with 9 8 7
temp=10 arrsys[0]=9 arrsys[1]=10 Instructions put 10 in the back. Understand the first
for (int j = i; J < Arrsys.length; J + +) {
if (Arrsys[i]>arrsys[j]) {
temp = Arrsys[i];
Arrsys[i] = Arrsys[j]; Modify the memory to move the small to the front
ARRSYS[J] = temp; Modify the memory to move the big to the back.
}
}
}

}
************************************
b Java for array sorting operation method, provides a method Java.util.Arrays.sort (array name)


3 example, array copy (tells about the power of the Java API)
A copy of the array, the parameters passed by the method, the source array, the target array, the length of the copy, and its own development
b Java provides support for system.arraycopy (source array, start point of source array, target array, start point of target array, copy length)


4 Summary
The reference pass of the array is the use of heap memory, you can pass an array into the method, pass the right time do not need to write [], directly write the name.




Three new features of Java support for arrays
1 variable parameters (one method can pass one parameter, or n parameters)
A definition Format
b Example
C Note: After all the variable parameters are taken over, they are saved as an array, so the direct array is accepted.
******************************
Variable parameters
public static void Ncanshu (Object ... array) {

foreach Loop
for (Object Obj:array) {
System.out.print (obj+ "* *");
}

For loop
for (int i = 0; i < Array.Length; i++) {
if (i==0) {
System.out.println (Array[i]);
}
}

}
******************************




2 foreach Output
Array output: The method that is provided after jdk1.5 is typically used for loop output.


3 random fetch, randomly assigns the characters in the character variable b array to the two-dimensional character array A (4 is the length of the B array)
A[I][J] = b[(int) Math.floor (Math.random ())]


Method: The This keyword calls the constructor method
A constructor method calls other overloaded methods, does not apply to construct method names, but instead uses the this (parameter list) Form, according to the parameter list, select the appropriate construction method.
1) A method with this, re-call this method (for the form otherwise does not meet the requirements, and then re-enter)
2) Two methods in this, one method calls another


******************************
This call
public int fun1 (int a, int b) {
return a+b;
}
public int fun2 (int a,int b) {
Return This.fun1 (A, b); No, this is also possible.
}
******************************

Javase Course Chapter fourth the methods, arrays, Java new features

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.