So I populated my arraylist in my constructor, called Dispenser, which holds 20 of an object I made, called FoodInfo, in this code:
This way my program can easily construct and populate a Dispenser and it's array of objects, say something like Chips or Pepsi.
The problem is when I try to make the first method in Eclipse it kept giving me an error. It won't identify the ArrayList I created, itemArray, so I can not call it.
I'm wondering if there is a way to keep the logic of creating and populating the array within my constructor or if I am simply going to have to populate it outside of the constructor in order to access. Basically why will my removeItem method not recognize the itemArray I created previously?
I hope everything makes sense.
public Dispenser(FoodInfo newItem) {
ArrayList<FoodInfo> itemArray = new ArrayList<FoodInfo>(20);
for (int i=0; i>=20; i++)
itemArray.add(newItem);
}
This way my program can easily construct and populate a Dispenser and it's array of objects, say something like Chips or Pepsi.
The problem is when I try to make the first method in Eclipse it kept giving me an error. It won't identify the ArrayList I created, itemArray, so I can not call it.
public void removeItem() {
itemArray.remove(itemArray.size()-1);
}
I'm wondering if there is a way to keep the logic of creating and populating the array within my constructor or if I am simply going to have to populate it outside of the constructor in order to access. Basically why will my removeItem method not recognize the itemArray I created previously?
I hope everything makes sense.