J2SE basic compaction series-static import, foreach loop, variable parameters

Source: Internet
Author: User

1: static import is very simple, for example:
[Java]
Import static java. lang. Math .*
In this way, you can directly use various methods in Math, such as abs, instead of using Math. abs.

2: variable parameters ()
First, let's look at the example:

[Java
Public class TestVarArgus {
Public static void dealArray (int... intArray ){
For (int I: intArray)
System. out. print (I + "");

System. out. println ();
}

Public static void main (String args []) {
DealArray ();
DealArray (1 );
DealArray (1, 2, 3 );
}
}
The output is:
[Java] view plaincopy
1
1 2 3

Then, the specific explanation is reproduced, but the original link cannot be found:
Variable parameters allow a method to use multiple parameters of the same type without determining the number of parameters during compilation. The actual result can be compared with the following code. To support the uncertainty of method parameters during runtime, the caller often needs to construct a Collection or Array for methods that do not support Varargs, add all the variable parameters to the Collection or Array and pass them to the method. This is less troublesome for method callers that support Varargs. The functions completed in these two sections are not much different from the final results. However, compared with the code readability and maintainability, the methods supporting Varargs are obviously less procrastinating and redundant. In addition, if concatMapStringWithoutVarargs is a method with a high probability of use, the value of the Method Supporting Varargs is more obvious in terms of ease of use.
/******************* Not to use Varargs ***************** ******************/
List <String> contentList = newArrayList <String> ();
ContentList. add ("value1 ");
ContentList. add ("value2 ");
ConcatMapStringWithoutVarargs (prefix, suffix, contentList );
/*************************************** ****************/
 
 
/******************* Use Varargs ****************** *****************/
ConcatMapStringWithVarargs (prefix, suffix, "value1", "valu2 ");
/*************************************** ****************/
 
Usage
Variable type... variable name
The Varargs Parameter definition is simple. The following is a constructor that uses variable parameters.
Public Guitar (String builder, String model, String... features );
String... features indicates that the constructor can accept the features variable of the variable. The following two constructor functions are OK.
Guitar guitar = newGuitar ("Martin ",
"HD-28V ",
"Hot-rodded by Dan Lashbrook ",
"Fossil Ivory Nut ",
"Fossil Ivory Saddle ",
"Low-profile bridge pins ");
Guitar guitar = newGuitar ("Bourgeois ",
"OMC ",
"Incredible flamed maple bindings on this one .");
The user may not even pass any value to features
Guitar guitar = newGuitar ("Bourgeois ",
"OMC ");
If you understand the nature of Varargs parameters, you can easily use them. In fact, the compiler will convert the Varargs parameter into an array of the corresponding type. For example
The compiler interprets public Guitar (String builder, String model, String... features)
Public Guitar (String builder, Stringmodel, String [] features)
Whether it is a for loop for features or a foreach loop, it is OK.
For (int I = 0; I <features. length; I ++ ){
String feature = features [I];
......
}
For (String feature: features ){
......
}
You can also convert the Varargs parameter accordingly.
Private List features;
This. features = java. util. Arrays. asList (features );
Since the variable length parameter supports 0 to N parameters, that is to say, the number of input parameters is 0 in the method, developers need to consider the marginal condition where the number of parameters is 0.
Public static int max (int... values ){
If (values. length = 0 ){
Thrownew IllegalArgumentException ("No values supplied .");
}
Int max = Integer. MIN_VALUE;
For (int I: values ){
If (I> max)
Max = I;
}
Return max;
}
 
Limits
When using the Varargs parameter, pay special attention to the following two points:
1. A method can only define one Varargs parameter. The following definition methods cannot be used during compilation.
Public Guitar (String builder, String model,
String... features, float... stringHeights) www.2cto.com
2. The variable length parameter can only appear at the end of the method definition. The following method definition compiler will prompt "the variable argumenttype String of the method concatMapStringWithVarargs must be The last parameter"
Public static voidconcatMapStringWithVarargs (Stringprefix, String... features, String suffix)
Summary
In fact, variable parameters are not a new thing. The printf function in C language already has such a good implementation. In java 1.5, it is a bit late to provide similar functions, however, it is better to be late than never.


Author: Allen_Zhao_2012

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.