This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to make it generic? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to make it generic?" Watch "How to make it generic?" New topic
Author

How to make it generic?

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
hi guys, I am practising with Trees
The code below is belong to a class called BinaryTree
please take a look at the bold portion, i did a Customer casting. As a result, this method is not generic and robust anymore because the whole BinaryTree class can only be used on Customer class but not other classes.
My searchkey is Comparable type which is used to uniquely identify the class.

what should i do so that I no need to use (Customer) casting??

I tried using tmpn1.getClass() tmpn1.getItem()).setKey((Comparable) (v1+v2)); but it doesn;t work.



Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Could you cast to KeyedItem?

BTW, your code still wouldn't work for any KeyedItem, but just for those that use Integers as keys, but I guess you are aware of that?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
hi,
yup the key item is in integer form



I have a customer class with name, address and a superclass KeyedItem with Comparable searchKey

I have 2 customers record (account id=20 and account id = 10)

my concern is to
1. add the account id together -> 30
2. copy the first customer record (account id=20) to account id=30

tt's the reason y i cast customer becoz I want to get the object of customer account id 20, change the search key to 30 only while retaining the original values of address and name.

but i find that my code is not robust
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why is the search key a Comparable instead of an Integer? Can you change the KeyedItem class?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make it generic?