Java Method Varargs overloading and ambiguity for parameter count

Question

What is wrong with the following code:

class Main { //from  w  w  w .  ja  va  2s  .  c o  m
 
  static void myTest(int ... v) { 
    for(int x : v) {
      System.out.print(x + " ");
    } 
    System.out.println(); 
  } 
 
  static void myTest(int a, int... v) { 
    for(int x : v) {
      System.out.print(x + " ");
    } 
    System.out.println(); 
  } 
 
 
  public static void main(String args[])  
  { 
    myTest(1, 2, 3);  
  } 
}


myTest(1, 2, 3);  //error
The method myTest(int[]) is ambiguous for the type Main

Java cannot decide which method to use, myTest(int...v) or myTest(int a, int... v).




PreviousNext

Related