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]
# 1
Are you trying to cast to the primitive wrappers or to the primitives themselves? Either way you can create a single convenience method to do the conversion for you using an if-else for each type.
kablaira at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...
# 2
You should consider using a linked list of Number or other appropriate data type. You should also consider refraining from storing objects of different types in a single Collection.~
yawmarka at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...
# 3
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.
Israfela at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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?

yawmarka at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

Israfela at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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)?

yawmarka at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...
# 7
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.
Israfela at 2007-7-13 12:51:45 > top of Java-index,Java Essentials,Java Programming...