public MonomialPolynomial(double... coeffs){
List<IMonom> monoms = new ArrayList<IMonom>();
for (int i = 0; i < coeffs.length; i++) {
// zero coeffs might yaffect the degree
if(coeffs[i] != 0)
monoms.add(new Monom(coeffs[i], i));
}
super.monoms = monoms;
}
Why do people write double... and not [] double when they mean to array?
Is there a special meaning for this?
double...declares it a “var args” parameter — inside your method it’s identical todouble[]but for the caller, it’s much easier to call with varying numbers of arguments (hence var args) without the need to explicitly create an array:Without var args:
With var args:
Edit: One thing to watch out for with var args, only the last argument of a method can be a var args argument. This guarantess there’s no ambiguity when calling a method, e.g.
foo(int a, int b...)is unambigious, because the first argument will always be assigned toaand anything after that will be go into ofb.foo(int a..., int... b)on the other hand is ambigious, there’s no way to tell iffoo(1, 2, 3)meansa={1} and b={2, 3}ora={1, 2} and b={3}.