2

I've got two overloaded constructor methods in a class (from UJMP package):

DefaultSparseIntMatrix(long... size)

and

DefaultSparseIntMatrix(int maximumNumberOfEntries, long... size)

I want to call the second one with the int, but the compiler gives this error:

reference to DefaultSparseIntMatrix is ambiguous

This is how I call it right now:

Matrix m = new DefaultSparseIntMatrix((int) (vertices.length*50), (long) vertices.length, (long) vertices.length);

How can I force the first parameter to be an int, and not a long? I know the other way around, just cast to a (long) , but I need it to be an int.

demongolem
  • 9,474
  • 36
  • 90
  • 105
floorish
  • 706
  • 6
  • 18

3 Answers3

2

try

Matrix m = new DefaultSparseIntMatrix((int) (vertices.length*50), new long[]{vertices.length,vertices.length});

the vararg is syntactic sugar for an array and it also allows passing in an array directly

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
2

Calling a function with a list of longs, or an int followed by a list of longs, is just asking for trouble, especially because java will automatically cast an int into a long for you. (In other words, what you have passed into that constructor would work for either one).

If you are dead set on this parameter pattern (which I strongly suggest you change), You are going to have to take in type Integer (as opposed to type int), and explicitly pass in an Integer object into the function.

In otherwords, try this:

DefaultSparseIntMatrix(Long... size)

and

DefaultSparseIntMatrix(Integer maximumNumberOfEntries, Long... size)
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
  • I know, but I can't change the package, just have the jar. However Integer doesn't work either unfortunately, still ambiguity error... Thanks for the answer anyway – floorish Nov 06 '11 at 22:24
  • Yup, if you only have the jar, your best bet is ratchet freak's idea. That's a terrible design of parameters though. – Leif Andersen Nov 07 '11 at 00:01
1

I would use the builder pattern and use something like this, which is as easy to use as a constructor and clearer in its purpose:

Matrix m = new DefaultSparseIntMatrixBuilder().withMaxNumberOfEntries(vertices.length * 50)
                                              .withSize(vertices.length, vertices.length)
                                              .build();

The build method calls a private constructor of DefaultSparseIntMatrix with the builder itself as unique argument, and the constructor gets the data from the builder.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255