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();
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • possible duplicate of [Is it possible to use a primitive type (int) in as a generic type in Java?](http://stackoverflow.com/questions/414408/is-it-possible-to-use-a-primitive-type-int-in-as-a-generic-type-in-java) – Joe May 09 '14 at 12:43
  • Unrelated to your question, but `e == v || v == e` is the same as just `e == v`. – user253751 May 09 '14 at 12:46

3 Answers3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 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`.) – Jon Skeet May 09 '14 at 12:51
  • @JonSkeet As you mentioned, its ugly. The problem is that if you do `verify(new double[] { 1, 2, 3 }, 1)` it will fail. – Peter Lawrey May 09 '14 at 12:53
  • 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". – Jon Skeet May 09 '14 at 12:55
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...

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40