Java Tutorial/Class Definition/Varargs
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Demonstrate variable-length arguments.
- 2 Demonstrating variable-length arguments
- 3 Limiting the object Types in a Variable Argument List
- 4 Make methods that have unspecified number of parameters:pass an array of Objects
- 5 Methods Accepting a Variable Number of objects
- 6 Overloading Vararg Methods
- 7 Use varargs with standard arguments.
- 8 Using varargs with standard arguments
Demonstrate variable-length arguments.
class VarArgs {
// vaTest() now uses a vararg.
static void vaTest(int... v) {
System.out.print("Number of args: " + v.length + " Contents: ");
for (int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
vaTest(10); // 1 arg
vaTest(1, 2, 3); // 3 args
vaTest(); // no args
}
}
Demonstrating variable-length arguments
A variable-length argument is specified by three periods (...).
static void yourMethodInVarargs(int ... v) {}
Number of args: 1 Contents: 10 Number of args: 3 Contents: 1 2 3 Number of args: 0 Contents:
Limiting the object Types in a Variable Argument List
public class MainClass {
public static void main(String[] args) {
System.out.println(average(1.0, 2.0, 3.0, 4.0, 5.0));
System.out.println(average(3.14, 1.414, 1.732));
System.out.println(average(new Double(7), new Double(8), new Double(9), new Double(10)));
}
// Average of a variable number of values
public static double average(Double... args) {
if (args.length == 0) {
return 0.0;
}
double ave = 0.0;
for (double value : args) {
ave += value;
}
return ave / args.length;
}
}
3.0 2.0953333333333335 8.5
Make methods that have unspecified number of parameters:pass an array of Objects
public class Main {
public static void main(String args[]) {
myMethod(new Object[] { "value 1", new Integer(2), "value n" });
}
public static void myMethod(Object parms[]) {
for (int i = 0; i < parms.length; i++)
System.out.println(parms[i]);
}
}
Methods Accepting a Variable Number of objects
public class MainClass {
public static void main(String[] args) {
printAll(2, "two", 4, "four", 4.5, "four point five");
printAll();
printAll(25, "Anything goes", true, 4E4, false);
}
public static void printAll(Object... args) {
for (Object arg : args) {
System.out.print(" " + arg);
}
System.out.println();
}
}
2 two 4 four 4.5 four point five 25 Anything goes true 40000.0 false
Overloading Vararg Methods
class MainClass {
static void vaTest(int... v) {
System.out.print("vaTest(int ...): " + "Number of args: " + v.length + " Contents: ");
for (int x : v)
System.out.print(x + " ");
System.out.println();
}
static void vaTest(boolean... v) {
System.out.print("vaTest(boolean ...) " + "Number of args: " + v.length + " Contents: ");
for (boolean x : v)
System.out.print(x + " ");
System.out.println();
}
static void vaTest(String msg, int... v) {
System.out.print("vaTest(String, int ...): " + msg + v.length + " Contents: ");
for (int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
vaTest(1, 2, 3);
vaTest("Testing: ", 10, 20);
vaTest(true, false, false);
}
}
Use varargs with standard arguments.
public class MainClass {
static void vaTest(String msg, int... v) {
System.out.print(msg + v.length + " Contents: ");
for (int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
vaTest("One vararg: ", 10);
vaTest("Three varargs: ", 1, 2, 3);
vaTest("No varargs: ");
}
}
Using varargs with standard arguments
A method can have "normal" parameters along with a variable-length parameter. However, the variable-length parameter must be the last parameter.
There must be only one varargs parameter.
int aMethod(int a, int b, double c, int ... vals) {}
One vararg: 1 Contents: 10 Three varargs: 3 Contents: 1 2 3 No varargs: 0 Contents: