This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing objects from Collection  RSS feed

Marc Kryzhan
Posts: 14
  • Mark post as helpful
  • send pies
  • 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();
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.
Jeanne Boyarsky
author & internet detective
Posts: 37242
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use an Iterator to loop through the Collection.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!