Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top