How do I find and cast Object as a subclass?
I've been stuck on this problem for my data structures class. Since I'm the only Java programmer there's little/no support.
I have a linked list of Object in order to hold numbers of diffrent types, but now I need to cast them as their subclass to perform math operations on them.
Is there a way to create a method or use existing methods to do this?
Checking instanceof for every Object could create several hundredif else statements which would be inefficient.
[498 byte] By [
Israfela] at [2007-10-2 14:30:09]

I'm creating a simple pushdown transducer.Since there's 10 Number types and a simple binary operator like +,-,*,/ will have 10^2 checks. That's 100*4 = 400 if else statments just for those 4 operators.
> I'm creating a simple pushdown transducer.
Sounds like you're creating an overly complicated one. ;o)
> Since there's 10 Number types and a simple binary
> operator like +,-,*,/ will have 10^2 checks. That's
> 100*4 = 400 if else statments just for those 4
> operators.
Nope. Stop and think for a moment. What commonality can you find between the Objects you're storing in your LinkedList?
I can easily cast them as Number, but you can't perform math operators on Number. Also, there's a symbol table holding user defined variables. I use instanceof String to check this and grabs the Object from the hashtable. So basically I have a linked list of Objects, all of Number, but each of diffrent types. I just need some way to cast them as their subclass.
> I just need some way to cast them as their subclass.
Not really; you need a way to get a numeric value of a common type. And this can be done a Number object. Excessive use of instanceof and downcasting (such as what you're describing) is typically considered a design problem. Can you think of a way to obtain a common numeric type from a Number object (hint: have a look at the API)?
I could ghetto-rig it by making everything double, but the integer math 5+5 would return 10.0 Other than that, Number has typeValue(). Nothing else.