We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Siblings of objects, best method
Page Index Toggle Pages: 1
Siblings of objects, best method? (Read 512 times)
Siblings of objects, best method?
Jan 24th, 2010, 4:35am
 
I am trying to figure out a way to have objects 'talk to' their siblings. Is there a well known 'best practice' way of doing this? I know I can loop through arrays, store the values, and pass them on to the next object. But I wonder if it is possible to do this in a more elegant way, preferably from inside the object itself (if this is not a big no-no for some reason). As far as I know, there is no generic method for this, like 'this.nextSibling' or something like that? But I'm thinking it would be possible to do this using an index-number, and then check if there are objects with index numbers above or below this one. But before I start testing this out, does anyone have any experience/hints on this topic? Is what I'm doing/thinking 'the wrong way'?
Re: Siblings of objects, best method?
Reply #1 - Jan 24th, 2010, 5:14am
 
There is a preferred way, and it is to pass to the object the array of objects that the object itself is contained in. For example:

Quote:
class TryNextDoor {
  boolean hasSugar;
  int myAddress;
  TryNextDoor theStreet[];
  TryNextDoor( boolean has, int address, TryNextDoor [] street ){
    hasSugar = has;
    myAddress = address;
    theStreet = street;
  }
  void ask(String message){
    if( hasSugar ){
      println( myAddress + ": I have some!" );
    }
    else{
      if( myAddress+1 < theStreet.length ){
        println( myAddress +  ": Nope. Try asking next door! " + message + myAddress );
        theStreet[myAddress+1].ask(message + myAddress);  
      }
      else {
         println( myAddress +  ": I guess no one has any. " + message + myAddress );
      }
    }
  }
}

TryNextDoor myNeighborhood[];

void setup(){
  myNeighborhood = new TryNextDoor[10];
  for( int i = 0; i < myNeighborhood.length; i++ ){
    myNeighborhood[i] = new TryNextDoor( false, i, myNeighborhood );
  }

  myNeighborhood[0].ask("");

}

void draw(){
}



Does that help clear things up at all?  Huh
Re: Siblings of objects, best method?
Reply #2 - Jan 24th, 2010, 5:32am
 
Yes, exactly what I was wondering about Smiley So by passing the whole array, you have access to all the properties/instance variables of all the objects (which would be all the siblings) in that array? I will play with your example for a while, and see if I get the results I am expecting, and if not I will come back for help! Smiley Would you say this is an advanced use of objects, or is it pretty basic stuff? Cheers! Cheesy
Re: Siblings of objects, best method?
Reply #3 - Jan 24th, 2010, 6:01am
 
What TfGuy44 said, except I would have used an ArrayList...
Or perhaps LinkedList.
If you use an Iterator over the collection, then you have a "natural" concept of next element, including hasNext(), next() or even nextIndex() for ListIterator.
Page Index Toggle Pages: 1