Java supplement

Front to tell you that some basic knowledge of Java, and today I did not say it in detail I add something before

String[ ] args

String [] args is the main function of the formal parameters can be used to obtain a user command line input into the parameters .

Specific methods are as follows: This is the main function to the input length args can be run directly.

It is mainly used when we execute a Java program (Java byte code file), you can specify a number of parameters.

Another said that, there is the name args can be changed, because it's just a parameter name.

Vararg

When we pass parameters to a function, and we are not sure if you want to pass a specific number of arguments, then this time you can use a variable-length parameters (which must be the same type). In fact, it is essentially an array.

1. a list of parameters which can have a variable length parameter, if multiple appear, first compile error occurs;

2. If the variable-length parameters and fixed parameters exist, then the variable-length argument must be the last face in the parameter list.

Specific use are as follows:

class Test02{
    public static void main(String[] a){
        multiArguments(1);
        multiArguments(1,2,3);
        multiArguments(1,2,3,4,5,6);
    }
    public static void multiArguments(int ... nums){
        for(int i = 0;i < nums.length;i++){
            System.out.print(nums[i] + " ");
        }
        System.out.println();
    }
}

foreach loop

format:

    for (loop loop variable data type name: iterables) {

         Loop body;

    }  

class Test03{
    public static void main(String[] args){
        multiArguments(1);
        multiArguments(1,2,3);
        multiArguments(1,2,3,4,5,6);
    }
    //传入的是不确定个数的int型参数
    public static void multiArguments(int ... nums){
        
	    for(int num:nums){
		    System.out.print(num);
	    }
	    System.out.println();
        }
    
    }

num is represented here in fact nums [i] i∈ [0 ~ nums.length), but shut off foreach specific behavior in the i. 

In addition, an array of objects which can be iterative, and can be accessed through the array subscripts, so that we can now equivalent to num nums [i]. , But does not represent all of the subscript iterable has supported some elements of access order iterables is more complex, so in order to avoid these complications have a foreach loop.
 There is a drawback foreach what does?
       1. It can only access the elements, the elements can not be modified.
       2.num just a backup nums [i], the change does not mean change num nums [i].
  So if we only access elements, then use the foreach loop will be easier.

Arrays类
  

1. static void Sort (int [] A) : This method is the specified array into ascending numerical order.
2. static int binarySearch (int [] A, int Key) : This method is to use a binary search algorithm to search for the specified int array to the specified value.
3. static int [] copyOf (int [] Original, the newLength int): This method is to copy the specified array, truncating or padding with zeros (if necessary), so that the copy has the specified length. 4. static String toString (int [] A): This method is to return the specified string representation of the contents of the array.

Multi return value processing

Java function returns only a single value only, if necessary disposable return multiple values, we need a plurality of values ​​which can be packaged in an array, set back also learned / mapping.

This is today's supplement, Thanks for reading. ^ - ^
       

 

 

Published 38 original articles · won praise 51 · views 1190

Guess you like

Origin blog.csdn.net/xweiwxh/article/details/104462065