I have a class Element<T> that contains a T and an int:
class Element<T>{
T obj;
int i;
// ...
}
When no T is stored, I would like to use obj like a pointer to an Element or an index:
obj = someElement;
obj = indexOf(someElement);
Of course, I can't do that because Java has no unions or variants (as in c++ variants) and has a strict type system.
Note that I would like to be able to access an Element through obj, but I have no desire to change it through obj, if that helps.
Questions:
- Is there any way to accomplish this without creating my own variant class or installing one?
- If so, what is the most memory efficient way to do this, since I will need to create many Elements?