Re: Collection and 'records', accessing, displaying attributes of elements of the collection
Thursday, April 10, 2003 11:16 AM
If you are using Collection to reference your
collection you have 2 options for examing each of its
elements. The first is using an Enumeration as
you are doing now. The second is to get an array of the
elements by calling the toArray () method of the
Collection and going through the elements of the
array with a loop.
If you have more specific knowledge of the type of the Collection, you generally would have more choices.
For example, if you are dealing with a List, you
can use the size () and get (int) methods
of the List to go through the elements of the collection (Your sample code implies that you are using an
ArrayList which is an implementation of List, so this approach should work for you).
The answer to the second part of your question may be that
you need to define the toString () method for each
of your own classes (classes whose instances may be elements
of the collection). The toString () method is exists for all Java Object by the virtue of the fact that Object defines it. If you don't override
toString () in your class, the system will go up your classes' parent hierarchy to find the method.