java - Why is my ArrayList length 0 in my mouseClicked() function? -
in processing program, added object global arraylist called items in draw function.
here class.
class obj { string name; obj(string name) { self.name = name; } } here draw function.
void draw () { items.add(new obj("bob")); } i tried printing size of items in mouseclicked() function console, keep getting 0.
void mouseclicked () { print(items.size()); } why?
the arraylist declared @ top of file after class:
arraylist<obj> items = new arraylist<obj>();
there couple of things aren't quite there yet:
self.name = name;shouldthis.name = name;- you're creating tons of objects (based on framerate), checking size of array in
mousecliked()(also might want useprintln()instead).
try this:
arraylist<obj> items = new arraylist<obj>(); void setup(){ } void draw () { } void mouseclicked () { items.add(new obj("bob")); println("items size: " + items.size()); } class obj { string name; obj(string name) { this.name = name; } }
Comments
Post a Comment