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 › accessing array element of another class
Page Index Toggle Pages: 1
accessing array element of another class (Read 1652 times)
accessing array element of another class
May 29th, 2010, 3:07am
 
hi,

in my program i have a class cell which holds data in an array. I have another class which is an array of agents. They have an ID (1, 2, 3...).  I want the agents to access the index of the cell array equal to their id.

should the following code work?...it doesn't appear to.

Cell:
float [] array = new float [variable];

the cells form a grid, which is an array of cells.
So to access the element = to agent id of the cell at agent position:
agent calls: grid[cellX][cellY].array[id-1]

hope that is clear - happy to expalin further if required.
thanks for any help.
Re: accessing array element of another class
Reply #1 - May 29th, 2010, 5:00am
 
I think you need to give us a bit more context.  If your agent class contains "an array of agents" what does the reference to the 'id' property of the Agent class refer to in this line:

grid[cellX][cellY].array[id-1]

If however you have an array of Agent objects then the above might make more sense...

Probably easier if you just posted some code Wink
Re: accessing array element of another class
Reply #2 - May 29th, 2010, 6:02am
 
ehm... the agent has a property called id, and needs to find the appropriate value IN the cell class at index equal to the Agents object id?

that's array[id] alright.

But I'm really unsure on what this cell business is. If you want all values returned from the arrays in all cells at the agent's id index, you'd just loop through the whole array of cells? "grid" is a two-dimensional array of Cell objects, right?
Re: accessing array element of another class
Reply #3 - May 29th, 2010, 7:35am
 
Yes: "the agent has a property called id, and needs to find the appropriate value IN the cell class at index equal to the Agents object id?"

so, my problem is elsewhere!

thanks for the help/clarity.


Re: accessing array element of another class
Reply #4 - May 29th, 2010, 8:24am
 
So you haven't got a class that contains an array of Agents, but an array that contains Agent objects?  This is why posting some code would have clarified your description...  So presumably this code is being called from within the agent class:

grid[cellX][cellY].array[id-1]

If your agent IDs start at 1, as per your description, the id-1 looks like the right index.  I guess the next question is whether 'grid[cellX][cellY]' returns an instance of a Cell object.  I.e. is every index of the 2D array populated with an instance of a Cell?  And is the array within each Cell declared and populated with a number of elements equal to the number of agents?
Page Index Toggle Pages: 1