java - Declaration of variable size dynamic array of objects in processing -
i want ask question processing. have user defined object class, point, , have 2 data members x , y both declared float. have class recognizer. need create constructor of recognizer , inside define object of point[] point0, point[] point1
, etc... dynamically.
point [] point0={new point(137,139),new point(50,63),..., new point(78,5)}; point1[],point2[],...,pointn[]
how else can add values point0 having values of corresponding x , y coordinate.
also note size of each object point1,point2,..., pointn
different.
how can achieve above goal?
how using 2 dimensional array ?
point [] points0={new point(137,139),new point(50,63),new point(78,5)}; point [] points1={new point(147,139),new point(60,63),new point(79,5)}; point [] points2={new point(157,139),new point(70,63),new point(80,5)}; void setup(){ point[][] pointlists = {points0,points1,points2}; recognizer r = new recognizer(pointlists); } class point{ float x,y; point(float x,float y){ this.x = x; this.y = y; } string tostring(){ return "point("+x+","+y+")"; } } class recognizer{ point[][] data; recognizer(point[][] data){ this.data = data; for(int = 0 ; < data.length; i++){ println("processing points list" + i); for(int j = 0; j < data[i].length; j++){ println("processing point["+j+"] of list " + + " : "+data[i][j]); } } } }
and arraylist version:
import java.util.arrays; arraylist<point> points0 = new arraylist<point>(arrays.aslist(new point[]{new point(137,139),new point(50,63),new point(78,5)})); arraylist<point> points1 = new arraylist<point>(arrays.aslist(new point[]{new point(147,139),new point(60,63),new point(79,5)})); arraylist<point> points2 = new arraylist<point>(arrays.aslist(new point[]{new point(157,139),new point(70,63),new point(80,5)})); void setup(){ arraylist<arraylist<point>> pointlists = new arraylist<arraylist<point>>(); pointlists.add(points0); pointlists.add(points1); pointlists.add(points2); recognizer r = new recognizer(pointlists); } class point{ float x,y; point(float x,float y){ this.x = x; this.y = y; } string tostring(){ return "point("+x+","+y+")"; } } class recognizer{ arraylist<arraylist<point>> data; recognizer(arraylist<arraylist<point>> data){ this.data = data; for(int = 0 ; < data.size(); i++){ println("processing points list" + i); for(int j = 0; j < data.get(i).size(); j++){//write in nicer way, many calls, might want reusable variable here println("processing point["+j+"] of list " + + " : "+data.get(i).get(j)); } } } }
Comments
Post a Comment