Wednesday, November 23, 2011

Class Definition » Varargs

Demonstrating variable-length arguments
A variable-length argument is specified by three periods (...).
static void yourMethodInVarargs(int ... v) {}
yourMethodInVarargs( ) can be called with zero or more arguments.
v is implicitly declared as an array of type int[ ].
Inside vaTest( ), v is accessed using the normal array syntax.
public class MainClass {

  // vaTest() now uses a vararg.
  public 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(123)// 3 args
    vaTest()// no args
  }
}
output
Number of args: 1 Contents: 10 
Number of args: 3 Contents: 1 2 3 
Number of args: 0 Contents:
 
 
 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) {}
int aMethod(int a, int b, double c, int ... vals, double ... morevals) { // Error!
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: "123);
    vaTest("No varargs: ");
  }
}
output
One vararg: 1 Contents: 10 
Three varargs: 3 Contents: 1 2 3 
No varargs: 0 Contents:

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();
  }
}
output
2  two  4  four  4.5  four point five
25  Anything goes  true  40000.0  false
 
Limiting the object Types in a Variable Argument List 
public class MainClass {
  public static void main(String[] args) {
    System.out.println(average(1.02.03.04.05.0));
    System.out.println(average(3.141.4141.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;
  }
}
output
3.0
2.0953333333333335
8.5
 
 
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(123)// 3 args
    vaTest()// no args
  }
}
 
 
 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: "123);
    vaTest("No varargs: ");
  }
}
 
 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(123);
    vaTest("Testing: "1020);
    vaTest(true, false, false);
  }
}

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]);
  }
}
 
http://www.java2s.com/Tutorial/Java/0100__
Class-Definition/Catalog0100__Class-Definition.htm

No comments:

Post a Comment