5.2 Iterating Over Variable-Length Argument Lists
All this varargs business is
well and good, but unless you can actually
use them in your methods, it's obviously just eye-candy and window
dressing. However, you can work with vararg parameters just as you do
an array, making usage a piece of cake.
5.2.1 How do I do that?
Make sure you read "Creating a Variable-Length Argument List," which
lets you know the most important piece of information relating to vararg
methodsvariable-length arguments are treated just as arrays. So, continuing
with the previous example, you could do something like this:
Chapter 7.This isn't particularly sexy, but it should get the point across. As another
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth,
GuitarInlay fretboardInlay, GuitarInlay
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);
}
}
example, here's a simple method that calculates the maximum from a set
of numbers:NOTEThis example is yanked straight out of Java in a Nutshell, Fifth Edition (O'Reilly).
Simple enough, right?
public static int max(int first, int... rest) {
int max = first;
for (int i : rest) {
if (i > max)
max = i;
}
return max;
}
5.2.2 What about...
...storing variable-length arguments? Since the Java compiler treats these
like arrays, an array is obviously a great choice for storage, as seen in
Example 5-1.
Example 5-2. Storing variable-length arguments as member variables
You could also store these in Java collection classes easily:
package com.oreilly.tiger.ch05;
public class Guitar {
private String builder;
private String model;
private float nutWidth;
private GuitarWood backSidesWood;
private GuitarWood topWood;
private GuitarInlay fretboardInlay;
private GuitarInlay topInlay;
private String[] features;
private static final float DEFAULT_NUT_WIDTH = 1.6875f;
public Guitar(String builder, String model, String... features) {
this(builder, model, null, null, DEFAULT_NUT_WIDTH, null, null, features);
}
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth, String... features) {
this(builder, model, backSidesWood, topWood, nutWidth, null, null, features);
}
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;
this.features = features;
}
}
NOTEThe java.util. Arrays class has several nice methods for working with arrays, all of
// Variable declaration
private List features;
// Assignment in method or constructor body
this.features = java.util.Arrays.asList(features);
which are of interest in varargs methods.