Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects): "Arbitrary Number of Arguments
You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array)."
Arbitrary Number of Arguments
You can use a construct called
varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array).
To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.
public Polygon polygonFrom(Point... corners) {
int numberOfSides = corners.length;
double squareOfSide1, lengthOfSide1;
squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x)
+ (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ;
lengthOfSide1 = Math.sqrt(squareOfSide1);
// more method body code follows that creates
// and returns a polygon connecting the Points
}
You can see that, inside the method,
corners
is treated like an array. The method can be called either with an array or with a sequence of arguments. The code in the method body will treat the parameter as an array in either case.
You will most commonly see varargs with the printing methods; for example, this printf
method:
public PrintStream printf(String format, Object... args)
allows you to print an arbitrary number of objects. It can be called like this:
System.out.printf("%s: %d, %s%n", name, idnum, address);
or like this
System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);
or with yet a different number of arguments.
分享到:
相关推荐
configuring a server to relay messages or to add and remove e-mail accounts. POP POP stands for Post Office Protocol. Currently in version 3, also known as POP3, RFC 1939 defines this protocol. ...
One of the strong features of java is that it is follows a OOPs concept, and one of the feature of OOP in java is that, we can assign a subclass object or variable to the variable of the superclass ...
The book is designed to give a fillip to the concepts of java programming. Java programming has been variously conceptualized by experts across the world. The subject forms the basis for software ...
A toolbox of inference techniques, including message-passing algorithms, Monte Carlo methods, and variational approximations, are developed alongside applications of these tools to clustering, ...
The main reason for making a virtual function inline is to place its definition in the class, either for convenience or to document its behavior, e.g., for accessors and mutators. The -inl.h Files...
1 Introduction to Information Theory . . . . . . . . . . . . . 3 2 Probability, Entropy, and Inference . . . . . . . . . . . . . . 22 3 More about Inference . . . . . . . . . . . . . . . . . . . . . ...
these sentences refer to the ABL compiler’s allowance for parameter passing and the AVM’s possible response to that parameter passing at run time: “ABL allows you to pass a dynamic temp-table ...
This exam focuses on Java Development Kit (JDK) 1.8 and is designed to assess candidates' understanding and skills related to this version of the Java programming language. #### Preparation Strategy...
- FIX: The method TCustomProp.GetPropNames moved to the public section and returns the list of published-property names of the complex flex-property. - FIX: The method TPropRefList.IndexOfRecode ...
### Pattern Recognition and Machine Learning **Pattern Recognition and Machine ... The book's comprehensive coverage and clear explanations make it a valuable addition to any machine learning library.
Information theory and inference, taught together in this exciting textbook, lie at the heart of many important areas of modern technology - communication, signal processing, data mining, machine ...
The ActionServlet (Command) passes the parameterized classes to ActionForm using the perform() method. Again, no more dreadful request.getParameter() calls. By the time the event gets here, the input ...
Reactive Streams in Java explains how to manage the exchange of stream data across an asynchronous boundary―passing elements on to another thread or thread-pool―while ensuring that the receiving ...
Java Web Services shows you how to use SOAP to perform remote method calls and message passing; how to use WSDL to describe the interface to a web service or understand the interface of someone else's...
"ROS By Example: A Do It Yourself Guide to the Robot Operating System" is an invaluable resource for anyone interested in learning ROS and applying it to build advanced robotic systems. Through its ...
**Bishop's "Pattern Recognition and Machine Learning"** is a comprehensive and authoritative text that serves as both an introduction to the field of machine learning and a reference for advanced ...
The chapter introduces programming as a set of instructions, or statements, written in a specific programming language that a computer follows to convert raw data into meaningful information....
A way of passing a request between a chain of objects Command Encapsulate a command request as an object Interpreter A way to include language elements in a program Iterator Sequentially access...