• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing objects from Collection

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
name.setName(itemName);
String gotName = name.getName();
inventory.toArray();
inventory.
}
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) {
System.out.println("Store::addInventory()");
InventoryItem i = new InventoryItem(name, type, cost, price);
inventory.add(i);

}
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.
Marc
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc,
You would use an Iterator to loop through the Collection.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic