1
static void m(int ...x,double ...y){}

I'm try to use this method. But it compile error.I want to know the reason.

int x[]=new int[2];
double y[]=new double[2];

I make two arrays like this and i want parse them to method m.

m(x,y); like this.
Udeesha Induwara
  • 605
  • 1
  • 10
  • 20

3 Answers3

5

You cannot have double varargs like void m(int ...x,double ...y){}.

Instead you can have void m(int[] x, double[] y){}.

tbsalling
  • 4,477
  • 4
  • 30
  • 51
4

Reading from official documentation:

Varargs can be used only in the final argument position.

Oracle documentation on Varargs

Luca Tampellini
  • 1,759
  • 17
  • 23
2

You can not have double varargs but the key question is "why this is not possible?" because the restriction does not come from Java itself. For example consider this two cases:

  1. If you have someMehod(Shape ...shape, Square ...square) -> you don't know where the Shape args stop or where Square args begin.
  2. If Square extends Shape-> the Shape class can exist in ...shape and ...square args due the polymorphism and this is quite confusing.
dim
  • 992
  • 11
  • 26