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:

  1. self.name = name; should this.name = name;
  2. you're creating tons of objects (based on framerate), checking size of array in mousecliked() (also might want use println() 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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -