Discussion > Bug: ArrayList doesn't work

Problem: ArrayList doesn's seem to work.

Workaround: In the meantime that this is fixed, simple array does the trick most of the times, by making these substitutions:

Declarations:
ArrayList trees = new ArrayList(5);
can be substituted by a big array and an integer for its size:
Tree[] trees = new Tree[500];
int treesSize = 0;

Get:
b = (Branch)branches.get(i);
can be substituted in array access:
b = branches[i];

Add:
trees.add(new Tree(new Coordinate(width/2, height - 10)));
can be substituted by inserting in the array and increasing the size:
trees[treesSize]=new Tree(new Coordinate(width/2, height - 10));
treesSize++;

Remove:
trees.remove(i);
can be substituted by shifting the array elements and decreasing the size:
for(int j=i; j < treesSize-1; j++) {
trees[j] = trees[j+1];
}
treesSize--;

Example - before and after (example sketch courtesy of Jonny Stutters):

Before (with ArrayList):

ArrayList trees = new ArrayList(5);
int NUMGENERATIONS = 7;


void setup() {
size(1100, 400);
}


void draw() {
Tree t;
if(trees.size() == 0) {
trees.add(new Tree(new Coordinate(width/2, height - 10)));
}
background(133,221,254);
strokeWeight(10);
stroke(187,209,66);
line(0, height - 5, width, height - 5);
for(int i=0; i < trees.size(); i++) {
t = (Tree)trees.get(i);
t.draw();
if(t.dead && t.colorOffset > 200) {
trees.remove(i);
}
}
}


class Tree {
ArrayList branches = new ArrayList(10);
ArrayList fruits = new ArrayList(10);
float x, y;
boolean dead = false;
int colorOffset = 0;

Tree(Coordinate c) {
this.x = x;
this.y = y;
branches.add(new Branch(this, c, -1, 0, 1));
}

void draw() {
Branch b;
Fruit f;
for(int i = 0; i
998) {
colorOffset = 10;
dead = true;
}
if(dead) {
colorOffset += 3;
}
}
}


class Branch {
Coordinate s, e;
float dy, dx;
int l = 0, g, fruitSize = 1;
boolean growing = true, madeFruit = false;
Tree p;

Branch(Tree parent, Coordinate start, float dy, float dx, int generation) {
p = parent;
s = start;
e = new Coordinate(s.x, s.y);
this.dy = dy;
this.dx = dx;
g = generation;
}

void grow() {
if(growing) {
float pDiv;
e.x += dx;
e.y += dy;
l++;
pDiv = (float)g * sq((float)l/100);
if(random(1) < pDiv) {
if(g < NUMGENERATIONS) {
p.branches.add(new Branch(p, e, random(-3, -1), random(-3, 0), g+1));
p.branches.add(new Branch(p, e, random(-3, -1), random(0, 3), g+1));
}
growing = false;
}
}
}

void draw() {
strokeWeight(NUMGENERATIONS-g);
stroke(143 - p.colorOffset, 119 - p.colorOffset, 91 - p.colorOffset);
line(s.x, s.y, e.x, e.y);
if(growing == false && g == NUMGENERATIONS && madeFruit == false) {
p.fruits.add(new Fruit(e));
madeFruit = true;
} else if(growing == false && g > 2) {
noStroke();
fill(42 - p.colorOffset, 191 - p.colorOffset, 26 - p.colorOffset);
ellipse(e.x, e.y, 5, 10);
}
}
}


class Fruit {
float dy = 0;
Coordinate l;
int fruitSize = 1;
boolean dead = false;

Fruit(Coordinate location) {
l = new Coordinate(location.x, location.y);
}

void grow() {
if(fruitSize < 7) {
fruitSize++;
} else {
if(random(1) > 0.9) {
dy = 10;
}
}
if(l.y < height - 10) {
l.y += dy;
} else if(l.y >= height - 10) {
if(random(1) > 0.02) {
dead = true;
} else {
trees.add(new Tree(l));
}
}
}

void draw() {
noStroke();
fill(184,22,40);
ellipse(l.x, l.y, fruitSize, fruitSize);
}
}

class Coordinate {
float x, y;

Coordinate(float x, float y) {
this.x = x;
this.y = y;
}
}


Example - After (ArrayList removed):
//ArrayList trees = new ArrayList(5);
Tree[] trees = new Tree[500];
int treesSize = 0;


int NUMGENERATIONS = 7;


void setup() {
size(1100, 400);
}


void draw() {
Tree t;
if(treesSize == 0) {
//trees.add(new Tree(new Coordinate(width/2, height - 10)));
trees[treesSize]=new Tree(new Coordinate(width/2, height - 10));
treesSize++;
}
background(133,221,254);
strokeWeight(10);
stroke(187,209,66);
line(0, height - 5, width, height - 5);
for(int i=0; i < treesSize; i++) {
//t = (Tree)trees.get(i);
t = trees[i];
t.draw();
if(t.dead && t.colorOffset > 200) {
//trees.remove(i);
for(int j=i; j < treesSize-1; j++) {
trees[j] = trees[j+1];
}
treesSize--;


}
}
}


class Tree {
//ArrayList branches = new ArrayList(10);
//ArrayList fruits = new ArrayList(10);
Branch[] branches = new Branch[500];
Fruit[] fruits = new Fruit[500];
int theBranchesSize = 0;
int fruitsSize = 0;

float x, y;
boolean dead = false;
int colorOffset = 0;

Tree(Coordinate c) {
this.x = x;
this.y = y;
//branches.add(new Branch(this, c, -1, 0, 1));<
branches[theBranchesSize]=new Branch(this, c, -1, 0, 1);
theBranchesSize++;


}


void draw() {
Branch b;
Fruit f;
for(int i = 0; i < theBranchesSize; i++) {
//b = (Branch)branches.get(i);
b = branches[i];
b.grow();
b.draw();
}
for(int i = 0; i < fruitsSize; i++) {
//f = (Fruit)fruits.get(i);
f = fruits[i];
f.grow();
f.draw();
if(f.dead) {
//fruits.remove(i);
for(int j=i; j < fruitsSize-1; j++) {
fruits[j] = fruits[j+1];
}
fruitsSize--;


}
}
if(dead == false && random(1000) > 998) {
colorOffset = 10;
dead = true;
}
if(dead) {
colorOffset += 3;
}
}
}


class Branch {
Coordinate s, e;
float dy, dx;
int l = 0, g, fruitSize = 1;
boolean growing = true, madeFruit = false;
Tree p;

Branch(Tree parent, Coordinate start, float dy, float dx, int generation) {
p = parent;
s = start;
e = new Coordinate(s.x, s.y);
this.dy = dy;
this.dx = dx;
g = generation;
}

void grow() {
if(growing) {
float pDiv;
e.x += dx;
e.y += dy;
l++;
pDiv = (float)g * sq((float)l/100);
if(random(1) < pDiv) {
if(g < NUMGENERATIONS) {
//p.branches.add(new Branch(p, e, random(-3, -1), random(-3, 0), g+1));
p.branches[p.theBranchesSize] = new Branch(p, e, random(-3, -1), random(-3, 0), g+1);
p.theBranchesSize++;

//p.branches.add(new Branch(p, e, random(-3, -1), random(0, 3), g+1));
p.branches[p.theBranchesSize] = new Branch(p, e, random(-3, -1), random(0, 3), g+1);
p.theBranchesSize++;
}
growing = false;
}
}
}

void draw() {
strokeWeight(NUMGENERATIONS-g);
stroke(143 - p.colorOffset, 119 - p.colorOffset, 91 - p.colorOffset);
line(s.x, s.y, e.x, e.y);
if(growing == false && g == NUMGENERATIONS && madeFruit == false) {
//p.fruits.add(new Fruit(e));
p.fruits[p.fruitsSize] = new Fruit(e);
p.fruitsSize++;

madeFruit = true;
} else if(growing == false && g > 2) {
noStroke();
fill(42 - p.colorOffset, 191 - p.colorOffset, 26 - p.colorOffset);
ellipse(e.x, e.y, 5, 10);
}
}
}


class Fruit {
float dy = 0;
Coordinate l;
int fruitSize = 1;
boolean dead = false;


Fruit(Coordinate location) {
l = new Coordinate(location.x, location.y);
}

void grow() {
if(fruitSize < 7) {
fruitSize++;
} else {
if(random(1) > 0.9) {
dy = 10;
}
}
if(l.y < height - 10) {
l.y += dy;
} else if(l.y >= height - 10) {
if(random(1) > 0.02) {
dead = true;
} else {
//trees.add(new Tree(l));
trees[treesSize]=new Tree(l);
treesSize++;
}
}
}

void draw() {
noStroke();
fill(184,22,40);
ellipse(l.x, l.y, fruitSize, fruitSize);
}
}

class Coordinate {
float x, y;

Coordinate(float x, float y) {
this.x = x;
this.y = y;
}
}

July 6, 2009 | Unregistered Commenterdavidedc