Create a class with a variable number of generic type

Xiidref :

I would like to create a class that can handle an undefined number of generic type is that even possible with java ?

The idea behind this weird question is to create a tuple with a size fixed in his contructor where each element at position i is of the type at the pos i%numberoftype.

Joachim Sauer :

No, types in Java have a fixed number of type parameters.

The closest you could get is to have a (finite) number of related classes:

class Tuple1<A> { ... }

class Tuple2<A, B> { ... }

class Tuple3<A, B, C> { ... }

And then provide a common class with factory methods:

class Tuples {
  static <A> Tuple1<A> of(A a) { ... }
  static <A, B> Tuple2<A,B> of(A a, B b) { ... }
  static <A, B, C> Tuple1<A,B,C> of(A a, B b, C c) { ... }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=418712&siteId=1