Home > Enterprise >  Variable Amount of Generics in Java
Variable Amount of Generics in Java

Time:01-24

In C you can create a tuple with a variable number of parameters. How would I implement something like this in Java without explicitly hard coding the amount of generics. I want to be able to do something like:

Tuple<Integer, Integer, String> t

but without forcing myself to use three items in a tuple.

CodePudding user response:

You can't, that's not supported in Java.

CodePudding user response:

Java is strongly typed language. And generics are provided by java to make more type checking language, and avoid Object parameter.

You can do this by,

Tuple<Object, Object, Object> t;

As in this you can insert any type of data in any order. But I suggest this is not good practice except you are better know how to use it.

Generics are strongly type checking in java, so you don't do this with generics.

  • Related