Java1.5 provides a new feature called VARARGS, which is the variable length parameter.
In the previous JDK version, the number of income parameters in the object is fixed after writing, and VARARGS provides variable length function, which is a bit similar to the parameter string of the main method [] args, we are in the command line When running, the number of ARGS is variable.
Writing using Varargs is such a public guitar (String Builder, String Model, String ... Features); uses an omitted ... declared features as a variable length parameter.
You declare the following method
Public Guitar (String Builder, String Model, String ... Features)
The compile time will be explained as:
Public Guitar (String Builder, String Model, String [] Features)
However, he has some restrictions. First, you can only use a omitted definition in a method, that is, only one variable parameter can be defined. The following definition is the illegal public guitar (String Builder, String Model,
String ... Features, Float ... StringHeights)
We can use the following method to get the variable length parameters:
Public Guitar (String Builder, String Model, Guitarwood BacksideSwood, Guitarwood Topwood,
Float Nutwidth, Guitarinlay Fretboardinlay, Guitarinlay Topinlay, String ... Features) {
THIS.BUILDER = BUILDER;
THIS.MODEL = Model;
THIS.BACKSIDESWOOD = BACKSIDESWOOD;
THIS.TOPWOOD = TOPWOOD;
THIS.NUTWIDTH = Nutwidth;
THIS.FRETBOARDINLAY = FRETBOARDINLAY;
THIS.TOPINLAY = TOPINLAY;
For (String Feature: Features) {
System.out.println (Feature);
}
}
We can also obtain the variable length parameter value, give Features directly to String [], or other collection types
// variable decaration
PRIVATE LIST FEATURES;
// Assignment In Method or Construction Body
THIS.FEATURES = java.util.arrays.aslist (features);
The above is taken from:
Http://www.onjava.com/pub/a/onjava/excerpt/javaadn_chap5/index.html
Http://www.onjava.com/catalog/javaadn/excerpt/javaadn_ch05.pdf