Skip to content

Instantly share code, notes, and snippets.

@stevenklise
Created October 18, 2012 18:37
Show Gist options
  • Save stevenklise/3913991 to your computer and use it in GitHub Desktop.
Save stevenklise/3913991 to your computer and use it in GitHub Desktop.
A simple example of setting and getting objects into an array
class Point {
int year;
int population;
float rateOfChange;
Point(int _year, int _population){
year = _year;
population = _population;
}
}
Point[] points;
void setup() {
size(400,400);
points = new Point[10];
println("SET 'points'");
for(int i=0; i < points.length; i++) {
int newYear = 1900 + i*5;
int newPop = (int)random(30000);
println(i + " " + newYear + " " + newPop);
// Create a new Point instance and set its year and population
Point newPoint = new Point(newYear, newPop);
// Store newPoint into the points array.
points[i] = newPoint;
}
println("GET from 'points'");
for(int i = 0; i < points.length; i++) {
// Get the ith Point from the points array and store it as 'tempPoint'
Point tempPoint = points[i];
// Print out attributes of tempPoint
println(i + " " + tempPoint.year + " " + tempPoint.population);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment