Problems with removing objects from an arraylist in processing

Marijn :

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();
        }
      }
    }
ajdaguese :

Your problem is probably because arrays and ArrayLists in Java get shallow copied when you pass them in a method. Essentially what this means is when you pass your ArrayList into a method, instead of making a new ArrayList, it just passes the memory location of your current ArrayList, so changes that take place inside of the method also take place outside of the method. You remove a particle from its own ArrayList which removes it from the top level ArrayList and since you do this to every particle, the top level ArrayList will have no particles left. To solve this you need to make a deep copy method that should look something like this:

ArrayList<d> deepCopy(ArrayList<d> original)
{
   ArrayList<d> copiedList = new ArrayList<d>();
   for(int i = 0; i < orginal.size(); i++)
   {
      copiedList.add(original.get(i);
   }
   return copiedList;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360689&siteId=1