• Post Reply Bookmark Topic Watch Topic
  • New Topic

Indexing subclasses  RSS feed

 
Bartholomew Benson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem where I want to give each subclass of a certain class an index number (I don't care what index numbers are given, as long as there is a one-to-one relationship between subclasses and index numbers and the index numbers don't skip). This number will be used to sort the subclasses as an intermediate step to what I want to achieve. I know I could do this:



But this quickly gets tedious when I'm looking at lots of subclasses. Plus, there's the off chance that I could mess up and assign an index twice to two different subclasses by accident. Is there a better way to do this? I read about Annotations and I'm wondering if they can help make my job easier in this case.
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could add your objects to a Collection such as an ArrayList
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about something like a static variable in the superclass and a method that increments it?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you clarify your requirement, because I interpreted the question differently to the two people who have answered so far.
Is the index related to each subclass (i.e. every instance of a given subclass will have the same index) or to each instance of a subclass (i.e. no two instances of a subclass will have the same index) ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder what the problem is you are trying to solve. I don't claim to be an expert, but this just seems like an odd requirement.
 
Bartholomew Benson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading a bit on Annotations, I figured out how to solve my problem:


It doesn't exactly solve the problem of accidentally giving two subclasses the same index, but at least it would be very easy to check, since the ordinal values are at the top of every class. It also occurred to me that I could just use a HashMap to map the subclass class objects to Integer index numbers. But I don't know if this would be better/faster than using annotations, or the other way around.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't mind manually assigning the numbers (and, off hand, I can't think of a way to avoid that, absent using a code-generator), then that annotation approach looks pretty complicated. Instead, you could have your classes initialize a parent class member to their index number and just access it directly. That way, even if you passed a reference to a subclass as a parent, you'd still get the subclass index number.

It's not pretty, but here's a way to do it:



Output is:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bartholomew Benson wrote:After reading a bit on Annotations, I figured out how to solve my problem:...

Did you read Fred's post? Because, like him, I'm wondering why you would want to do this.

It seems you've already decided how you're going to solve the problem before you've even started; and that's generally not a good way to go. Unfortunately, without some idea of what it is you're trying to solve - or why you think this is the best way - it's very difficult to offer any alternatives.

All I can suggest is that Java enums are unique by definition, and have an ordinal(). They can also implement interfaces. However, this may not be sufficient for your needs.

There are also other possibilities, such as instanceof or class equality (or hash) tests which could offer similar capability; but again, without knowing what you're trying to do, it's very hard to advise.

All I will say is this - That "sequencing" business just feels wrong to me: it's clunky, extra-linguistic and (I suspect) error-prone.

HIH

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!