`
javafox
  • 浏览: 55847 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

阅读更多
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.
分享到:
评论

相关推荐

    Java邮件开发Fundamentals of the JavaMail API

    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. ...

    JSP Simple Examples

    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 ...

    A Textbook of Java Programming

    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 ...

    Information Theory, Inference, and Learning Algorithms David J.C. MacKay

    A toolbox of inference techniques, including message-passing algorithms, Monte Carlo methods, and variational approximations, are developed alongside applications of these tools to clustering, ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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...

    电子书Information Theory Inference and Learning Algorithms

    1 Introduction to Information Theory . . . . . . . . . . . . . 3 2 Probability, Entropy, and Inference . . . . . . . . . . . . . . 22 3 More about Inference . . . . . . . . . . . . . . . . . . . . . ...

    Progress/OpenEdge语言手册

    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 ...

    1Z0-811 Exam Guide to Have a Cakewalk in Oracle Java SE Certific

    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...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - 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 Recogintion and Machine Learning

    ### 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, Inference, and Learning Algorithms

    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 ...

    外文翻译 stus MVC

    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: Concurrency with RxJava, Reactor, and Akka Streams

    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

    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.pdf

    "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 Pattern Recognition and Machine Learning

    **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 ...

    计算机英语第二版清华第5单元.pdf

    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....

    设计模式C++代码示例-含VC工程

    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...

Global site tag (gtag.js) - Google Analytics