Collection and 'records', accessing, displaying attributes of elements of the collection
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Marika_Ludmann
Posted On:   Thursday, April 10, 2003 10:41 AM

I have posted a question under JavaLanguage as follows: I have a Collection of objects. A list of buyers, each buyer object having id, name, address etc. Collection col = BuyerDAO.listBuyers(); OR ArrayList col = BuyerDAO.listBuyers(); I need to print these details out (using Swing). How can I convert the Collection or the objects within the collection to a String so I can print it? And how can I access indivual elements of the Collection? The col.get(1) didn't seem to work. And if I use col (Collection itself) it prints: [entity.Buyer@1d0af, entity.Buyer@bfc8e0] I am "newish" to Java, any help would be appreciated. I did get great help from someone, which involved using Iterator and a toString()   More>>

I have posted a question under JavaLanguage as follows:


I have a Collection of objects. A list of buyers, each buyer object having id, name, address etc. Collection col = BuyerDAO.listBuyers(); OR ArrayList col = BuyerDAO.listBuyers();
I need to print these details out (using Swing). How can I convert the Collection or the objects within the collection to a String so I can print it? And how can I access indivual elements of the Collection?
The col.get(1) didn't seem to work. And if I use col (Collection itself) it prints: [entity.Buyer@1d0af, entity.Buyer@bfc8e0]
I am "newish" to Java, any help would be appreciated.


I did get great help from someone, which involved using Iterator and a toString() method within the Buyer class.

However I was wondering if there is a better method to access elements of the Collection and to print elements of the collection in such cases. Also I need at times update attributes of the elements or only display certain attributes.
For example update only the first name of the buyer, where name = Ludmann.
Or display IDs only.

Can anyone help?
Or does anyone knows of any tutorials on the web that would be useful?

Thank you, Marika Ludmann

   <<Less

Re: Collection and 'records', accessing, displaying attributes of elements of the collection

Posted By:   Bahman_Barzideh  
Posted On:   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.
About | Sitemap | Contact