File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Accessing objects from Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing objects from Collection" Watch "Accessing objects from Collection" New topic

Accessing objects from Collection

Marc Kryzhan

Joined: Mar 17, 2004
Posts: 14
public class Store implements hw9.interfaces.StoreInterface {
private Collection inventory = new ArrayList();
public hw9.interfaces.InventoryItemInterface getInventoryItem(String itemName) {
// inventory.indexOf(itemName);
InventoryItem name = new InventoryItem();
String gotName = name.getName();
I have a Collection that is created called inventory. I believe I've created an okay method to add to the collection. To test I need to fix my above method. Problem is Collection methods do not have a get function. I'd like to access the methods in ArrayList but don't know how. Please suggest how?
p.s. Here's my method to add input into a collection as an object;
public void addInventory(String name, String type, double cost, double
price) {
InventoryItem i = new InventoryItem(name, type, cost, price);

InventoryItem () is a constructor in InventoryItem class which has the setName() and getName() methods. This is just there to teach me how to think in terms of protecting code I think. To really put into and get out of the collection all I would need I believe is the Store class.

Newbee to java.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33124

You would use an Iterator to loop through the Collection.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Accessing objects from Collection
It's not a secret anymore!