2

How can I replace those 2 functions with one using something like C++ tempates?

public void verify(final int[] array, final int v) {
    for ( final int e : array ) if ( e == v || v == e  ) return;
    abort_operation();
}

public void verify(final double[] array, final double v) {
    for ( final double e : array ) if ( e == v || v ==  e  ) return;
    abort_operation();
}
2

3 Answers 3

12

You can't, basically. Java generics don't work with primitive types. You could do it with reflection, but it would be ugly. You could also do it with the boxed types, like this:

public <T> void verify(T[] array, T value) {
   if (!Arrays.asList(array).contains(value)) {
       abortOperation();
   }
}

... but that would only work for Integer[] and Double[], not int[] and double[].

That's why the Arrays class has so many overloads for methods like binarySearch... if your method could have been made generic, so could those ones.

Fundamentally, generics are not the same as C++ templates. They cover a lot of the same use cases, but they're not the same, and you shouldn't be surprised to see some areas covered by one but not the other.

Sign up to request clarification or add additional context in comments.

2 Comments

You can if you use the Array reflection class.
@PeterLawrey: Yes, hence "You could do it with reflection" :)
2

You can if you use the Array class

public void verify(final Object array, final double v) {
    for(int i = 0, len = Array.getLength(i); i < len; i++)
        if(((Number) Array.get(array, i)).doubleValue() == v)
             return;
    abort_operation();
}

or

public void verify(final Object array, final Number v) {
    for(int i = 0, len = Array.getLength(i); i < len; i++)
        if(((Number) Array.get(array, i)).equals(v))
             return;
    abort_operation();
}

Note: all possible int can be represented as a double so you can make v a double without loss of precision.

This will work for all the numeric primitives and Number types. (Note: some long values cannot be converted to double without loss of precision)

If e == v then v == e must also be true.

3 Comments

Why not just take Number instead of double and use equals instead? (That might have odd effects around NaN etc, but that's less likely to be a problem than long.)
@JonSkeet As you mentioned, its ugly. The problem is that if you do verify(new double[] { 1, 2, 3 }, 1) it will fail.
Yes, you would need to make sure you pass in the right Number type. That's more likely to be an issue if you're using a primitive than a variable though, and I think I prefer it as a caveat to "long values may or may not work".
1

You cannot - the varying type is primitive, and Generics doesn't cover primitives. Maying in Java 10-11...

Mind you, you can use boxed types and it will work, if you use equals instead of ==, and accept the horrible performance and memory footprint penaly...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.