Loading...
Logo
Processing Forum
so this is were everyone is.....
 
this is following on from my post on the "old" discussion forum, which i'm still stuck on
 
an arrayList of agents delete an element from their arrayList (associates) when they find one of those associates. The problem is that when one agent does this the element is removed from the arrayList 'associates' for all of the agents in the same arrayList of agents. I want only the agent that has found the associate to remove the element from their arrayList 'associates'.
 
My agents are a class (referred to as ObjectC below) that form an arrayList (ObjectCs). There are two other classes in my program: ObjectA and ObjectB. ObjectA is composed of ObjectB and ObjectC. So all objects have the same arrayList 'associates'. As ObjectA and ObjectB are single objects i can set them up ok, as simply; add().
 
believe the problem is that i am "setting a reference to the original array rather than creating a true copy" for each agent in the first place. Can anyone advise as to how i should be setting up the arrayList 'associates' for each agent/ObjectC which is part of an arrayList (ObjectCs).
 
i hope that is clear - if not happy to try and explain clearer.

Thanks for any help.

code:

ObjectA [] objecta= new ObjectA[objecta];
void setup() {
//data is loaded from an xml file

objecta[i] = new ObjectA(id, color(H, S, B), area, new PVector(position.x, position.y));     //defines ObjectA
 //add associates to arrayList of ObjectA and ObjectB
    objecta[i].associates.add(asso1);
    objecta[i].objectb.associates.add(asso1);
    objecta[i].associates.add(asso2);
    objecta[i].objectb.associates.add(asso2);
}
////////////////////////////////////////////////////////////////////
class ObjectA{
  ObjectB objectb;
  ArrayList objectCs;
  PVector origin;
  ArrayList associates = new ArrayList();
  int objectcCount = population;

  ObjectA(int id, int colour, int Size, PVector loc) {
    objectb= new ObjectB (id, colour, Size, loc);                
    //ObjectC
    objectCs = new ArrayList();
    origin = loc.get();
    for (int i = 0; i < population; i++) {
      objectCs.add(new objectC(id, Size, origin, false, i, associates));
//i think this is were i go wrong passing the arrayList 'associates' to objectCs 
    }
  }
////////////////////////////////////////////////////////////////////
class ObjectB {
  int id;
  color colour;
  int Size;
  int x;
  int y;
  PVector position;
  ArrayList associates = new ArrayList();

  ObjectB(int identity, int c, int area, PVector loc) {  
    id = identity;
    colour = c;
    Size = area;
    position = loc.get();
    }
////////////////////////////////////////////////////////////////////
class objectC{
  ArrayList associates = new ArrayList();

objectC(int identity, int Size, PVector loc, boolean mode, int whom, ArrayList asso) {
  associates = asso;
  }

Replies(12)

Ok i've never been this confused in my entire life (actually that's not true, i once tried to learn Objective-C).
but just of the bat,
1) don't remove large parts of your code - things starts to make no sense then...
2) wtf is a agent and associate ? is this actually a programming term or something that has to do with your program?
3) You can't declare a variable in it's definition!
Copy code
  1. class ObjectA{
      ObjectB objectb;
      ArrayList objectCs;
      PVector origin;
      ArrayList associates = new ArrayList();
      int objectcCount = population;
those two last lines needs to be put inside the constructor! also you do this in every object...
So actually now that i think about it, your problem is probably there.

(I haven't yet had any coffee today so sorry if i'm not any constructive)
I am nearly as confused as bjordal, except I think that you can keep the last two lines as they are (even though I don't know where that 'population' variable comes from).
Having class names clearer than ObjectX would help a bit, too...
I see that in objectC (ah, not ObjectC?), you create a new associates list... then overwrite it in the constructor!
You probably want to iterate on the content of asso and .add() each element to associates.
Holy s*#@! you can declare a variable in it's definition (in a class), this is not cool.... how is this even possible, it's illegal in normal java as far as i know... also it feels like someone just stabbed my heart with a knife :(
sorry i wasn't clear in my post: to answer some of bjordal's comments
- i removed large parts of my code to help (as 1000+ lines) - though i see its just too abstract
 
ok, forget my code above as their is superflous stuff in there to my problem.
 
my program is made up of three classes. one is composed of the other two.
One of these 'inner' classes is an arrayList of objects.
As these form a composite class they all have the same variable: which is an arrayList called "associates"
 
question: 
how to copy the variable (arrayList "associates") to the inner class which is an arrayList of objects?
i want a true copy of the arrayList, not a reference of it.
 
if it still doesn't make sense then please just say so i can crawl in a hole and rethink. thanks.
how to copy the variable (arrayList "associates") to the inner class which is an arrayList of objects?
i want a true copy of the arrayList, not a reference of it.

As I wrote, iterate on original and use add() to copy the reference. See also Collections.copy, more convenient actually (doing that job for you).
In both cases, the array lists will be distinct (if you remove a reference from one, it will still be in the other) but the objects will be shared (ie. the lists hold references to these objects).
bjordal said
Holy s*#@! you can declare a variable in it's definition (in a class), this is not cool.... how is this even possible, it's illegal in normal java as far as i know... also it feels like someone just stabbed my heart with a knife
The ObjectA class has probably been written inside the main Processing sketch in which case it is an inner class so has access to the main class attributes e.g. population

You can take the knife out now Java is secure!


what i was referring too was that you can do this in processing:

Copy code
  1. class Something{
  2.       SomeOtherClass mySomeOtherClassObject = new SomeOtherClass();
  3.       public Something(){
  4.             mySomeOtherClassObject.doSomethingEvenThoughThisShouldGiveYouNullPointerError();
  5.       }
  6. }

Sorry my mistake but still ...

When an object of Something is created any attribute initialisation code is performed before the constructor code is executed so in your example line 2 will always be executed before the constructor so no null pointer error. This is standard Java not a Procecessing thing.



Really? that's really strange, from a c++ point of view :p
True this is not allowed in C++ where all initialization must be done in the ctor.

It is a really useful feature if you have multiple constructors, it saves you having to include the initializations in every constructor.
did some quick reading and it appears that the compiler copies the code into all constructors is runned from there... make a bit more sense now