• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make it generic?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the search key a Comparable instead of an Integer? Can you change the KeyedItem class?
 
reply
    Bookmark Topic Watch Topic
  • New Topic