Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trouble Accessing an ArrayList Populated in Class Constructor

$
0
0
So I populated my arraylist in my constructor, called Dispenser, which holds 20 of an object I made, called FoodInfo, in this code:
	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.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>