One of Java 1.5 features: Variable Length Parameter Function

Source: Internet
Author: User

 

 

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

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.