Java1.5 provides a function called varargs, which is a variable length parameter.
In previous JDK versions, the number of input parameters of a method in an object is fixed after being written, and varargs provides the variable length function, it is a bit similar to the main method parameter string [] args. When we run the command line, a handful of argS can be variable.
I. varargs is written as follows:
Public guitar (string builder, string model, string... features );
// Use the ellipsis (...) to declare features as a variable length parameter.
If you declare the following method:
Public guitar (string builder, string model, string... features)
It will be interpreted:
Public guitar (string builder, string model, string [] features)
However, there are still some restrictions.
Only one ellipsis can be used to define a method, that is, only one variable length parameter can be defined.
The following definition is invalid:
Public guitar (string builder, string model, string... features, float... stringheights)
2. We can use the following method to obtain the variable length parameter:
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 and assign features directly to string [] or other set types.
// Variable Declaration
Private list features;
// Assignment in method or constructor body
This. Features = java. util. arrays. aslist (features );
Http://www.onjava.com/pub/a/onjava/excerpt/javaadn_chap5/index.html
Http://www.onjava.com/catalog/javaadn/excerpt/javaadn_ch05.pdf