When objects are printed out, they are displayed as Strings by calling their toString() method. If a class does not have a toString() method, it will use the one from its super class. The default toString in the Object class just prints out the object reference (i.e. VehicleRecord@18a992f).
You can add a toString() method to your VehicleRecord class like so:
...or something similar, depending on what fields VehicleRecord has.
Joined: Oct 04, 2004
Many thanks for your response. Much appreciated.
Whilst I am aware of the toString( ) method I feel that as I am using a TreeMap I have already put the values in using put:
As I have then said a Collection these should just print out - as in the example in my book/notes.
When reading further though I note that sometimes the following is used:
Your code doesn't compile because you are trying to cast a Collection to a String, which isn't going to work. Take out the cast, and override toString() in the VehicalRecord class (as Stuart Gray suggests). That will sort out your compilation problem. [ April 25, 2005: Message edited by: Paul Sturrock ]
I am a bit concerned/confused with the semantics of your store(VehicleRecord) method. First, the VR passed in is completely ignored. Second, the method creates a new VR using methods that access individual VR attributes on the VehicleDB itself. Why does VehicleDB have getReg(), getMake(), getModel(), etc?
What I would have expected to see is something like this:Of course, I don't have the rest of the code so I'm missing quite a bit of context that would help explain why it is the way it is. I'm just giving you my view as I would have coded it based on my past experience. It could wildly deviate from the requirements you were given.
[ Trimmed unchanged code ] [ April 25, 2005: Message edited by: David Harkness ]
Joined: Oct 04, 2004
Many thanks for your response.
I have coded it just the way you stated above; in the end.
I was attempting to do a number of things. One of them was inheritance. I was seeing if it was feasible to use the Map and Set in an extended class as I had not seen any examples where this was implemented (it was just as good within an extended class as it is without one). We had just covered that subject so I was having a few questions going through my mind.
Thanks again for noticing it and pointing it out. Much appreciated.