I am fairly new to programming, and I can't get this to work. It is complicated to explain. There basically is a particle generator that generates a set number of particles. Those particles need to interact with each other, so they all have an ArrayList with all the particles. However, I don't want them to interact with themselves, so I want to remove the object from their ArrayList that is themselves. The storing class still has all the objects in his own Arraylist. The problem is that when I try to let the objects remove one object from their Arraylist, all the objects get removed and the ArrayList of the storing object is also empty. I know I don't have to do this, but I really want to know why it doesn't work and I think it is cleaner if it works this way than when I have to skip one object each time the interacting function runs. Because it is pretty complicated to describe, it is better to just look at the code. the code consists of just the code that matters and it is a simplified version. The line: l.remove(n); , is the line that screws everything up.
G g;
void setup()
{size(1000, 600);
g = new G(10);
}
void draw()
{
background(255);
g.displayg();
text(g.g.size(), 200, 200);
}
first class that is going to be stored in the g class
class D
{
int n;
ArrayList<D> l = new ArrayList<D>();
D(int input)
{
this.n = input;
}
void seTup(ArrayList<D> input)
{
l = input;
l.remove(n); //if you delete this line, everything works
}
void display()
{
fill(0);
text(this.n, 100+ 10* n, 100);
}
}
and the storing class
class G
{ int number;
ArrayList<D> g = new ArrayList<D>();
G(int inputnumber)
{this.number = inputnumber;
for(int i =0; i < number; i++)
{g.add(new D(i));
}
for(int i = g.size()-1; i>=0;i--)
{
D x = g.get(i);
x.seTup(g);
}
}
void displayg()
{
for(D i : g)
{//text(i.n, 100 + 5*i.n, 100);
i.display();
}
}
}