java - Creating an ArrayList and adding items -
im learning java , having problem arraylist.
firstly have class called item, create various item objects. have class catalogue array list , should hold list of item objects create. @ moment can manually add items catalogue invoking additem method on catalogue object , manually entering name of item object want add (item1 item2 item3 etc) wanted know if there way add items arraylist automatically each time create item object?
i should mention, list needs hold infinite amount of items, have not specified size in code. appreciated :) thanks
import java.util.arraylist; public class catalogue { private arraylist<item> catalogue; public catalogue () { catalogue = new arraylist<item>(); } public void addanitem(item item) { catalogue.add(item); } }
one way this, if passed catalogue constructor of item class, , once item set up, add item catalogue @ point.
it may this
public item(catalogue catalogue) { // set item here // add item catalogue catalogue.addanitem(this); }
Comments
Post a Comment