My code is something like this:
Code: Select all
particle = new Particle(x,y,mass);
tree = new BHTree();
tree.addParticle(particle);
This works. However, I want lots of particles. So I did this:
Code: Select all
particles = new Array();
tree = new BHTree();
particles[0] = new Particle(x1,y1,m1);
particles[1] = new Particle(x2,y2,m2);
particles[2] = new Particle(x3,y3,m3);
for(i = 0; i < particles.length; i++){
tree.addParticle(particles[i]);
}
This does not work. Confusingly (for me), this does work:
Code: Select all
particles = new Array();
tree = new BHTree();
particles[0] = new Particle(x1,y1,m1);
particles[1] = new Particle(x2,y2,m2);
particles[2] = new Particle(x3,y3,m3);
tree.addParticle(particles[0]);
tree.addParticle(particles[1]);
tree.addParticle(particles[2]);
What's going on? I'm guessing that one method passes the object itself, and one passes a copy of it*, but if one of you wizards could shed some light on this then I'd be very grateful.

* My OO programming skills are very limited, as is my knowledge of the correct terminology.