This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I would like advice please on a suitable structure to model the following situation.
A 'Box' contains an unlimited number of named 'Envelopes'.
Each 'Envelope' contains an unlimited number of 'RecordCards.
Each 'RecordCard' has a limited number (5) of DataItems.
A Box will be able to add and remove Envelopes.
An Envelope will be able to add and remove RecordCards
RecordCards will be able to change data held in the data items.
Data can be accessed using [Evelope][RecordCard][DataItems]
The Box can be serialized for persistence.
This is easily done in Python using Lists (which can hold mixed types) and the pickle methods for serialization but I would like to do it in Java for easy cross-platform distribution.
I don't understand why you would want a list to hold different things. A box only holds one thing - envelopes.
Envelopes only hold one thing - recordCards.
So, i would define a class called Box that contains a collection of envelopes. Then do the same for envelopes and recordCards, although you'd have to put in a check in your 'add' method to verify the size was under 5.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Nov 10, 2010
Thanks for the reply.
As to why I might want a list to hold different things, this is why I emphasised that I was coming from Python. Imagine that a user wants to add a new 'envelope' called "Advanced Brain Surgery Course Students" and then add Student Record Cards to the new 'envelope' in Python a new list could be created which had as its first element a string "Advanced Brain Surgery Course Students" and subsequent elements could be lists containing 'RecordCards' which are themselves lists containing the data items as their elements. These data items could also be different types - strings, integers etc. So to access individual DataItems one could just use something like : required_data_item = Box[Envelope_index] [RecordCard index] [DataItem index]
This makes getting the RecordCards for processing very easy as they are in the same list as the string which identifies the list.
Now as it seems that collections in Java cannot hold different types I am really asking what kind of objects would be best to use for the classes, ArrayLists, HashMaps or what, and an indication to how the data items could be accessed.