0

I want to implement some algorithm:

public class MaximumSubarrayAlgorithm<TArray extends List<T>, T extends Comparable<T>> 
{

public void DoSmth(TArray simple) {

    T t1 = simple.get(0);
    T t2 = simple.get(1);

    t1 = t1 + t2; // wrong: The operator + is undefined for the argument type(s) T, T

    return null;
}

}

How to require possibility to add instances of type parameters (and tell it to java compiler)? May be T is not Integer, it may be some other type, that support addition.

Alex T
  • 2,067
  • 4
  • 20
  • 27

1 Answers1

1

This is not supported in Java. You cannot use + on objects whose types you don't know, except to perform string concatenation; Java does not support operator overloading.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Workaround? Use concrete types? And rewrite algorithm, for example, for matrices? – Alex T Apr 20 '15 at 22:13
  • Yeah, use concrete types. Make your matrix data structure attached specifically to doubles or ints or whatever; don't try to generify it to different number types. – Louis Wasserman Apr 20 '15 at 22:25