File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make it generic?

 
Timothy Leong
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Timothy Leong
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the search key a Comparable instead of an Integer? Can you change the KeyedItem class?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic