Java 1.5 Tiger A Developers Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Java 1.5 Tiger A Developers Notebook [Electronic resources] - نسخه متنی

David Flanagan, Brett McLaughlin

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








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:


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);
}
}

Chapter 7.

This isn't particularly sexy, but it should get the point across. As another
example, here's a simple method that calculates the maximum from a set
of numbers:

NOTE

This example is yanked straight out of Java in a Nutshell, Fifth Edition (O'Reilly).


public static int max(int first, int... rest) {
int max = first;
for (int i : rest) {
if (i > max)
max = i;
}
return max;
}

Simple enough, right?


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


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;
}
}

You could also store these in Java collection classes easily:


// Variable declaration
private List features;
// Assignment in method or constructor body
this.features = java.util.Arrays.asList(features);

NOTE

The java.util. Arrays class has several nice methods for working with arrays, all of
which are of interest in varargs methods.


/ 131