GFTargetingBot

From Robowiki
Revision as of 20:16, 22 February 2008 by Pedersen (talk | contribs) (apples and 'containers of apples')
Jump to navigation Jump to search

I'm new to java, and my lack of understanding Java has me hung up on this key peice of code:

  buffer = statBuffers[distanceIndex][velocityIndex][lastVelocityIndex];

from my understanding; buffer is an array of 25 (ints), each bin being a counter of times visted. And statBuffers is a 4dimesional array of size: 5(int), 5(int), 5(int),and 25(int) : private static int[][][][] statBuffers = new int[DISTANCE_INDEXES][VELOCITY_INDEXES][VELOCITY_INDEXES][BINS]; so back to the problem code : buffer = statBuffers[distanceIndex][velocityIndex][lastVelocityIndex];

so the above "indexes" (0-4) should retrieve the values in thoses locations in the statBuffers arrays.. Am I wrong so Far ? It seems to me were assigning apples to oranges here? How does it work ? .. - Justin

int single[] = new int[ 5 ];
single is "an array of integers".
int double[][] = new int[ 5 ][ 5 ];
double is "an array of 5 arrays of 5 integers".


This means that each element in double[ n ] is itself "an array of 5 integers".


Let's change int to apple.
apple double[][] = new apple[ 5 ][ 5 ];
double is now "an array of 5 arrays of 5 apples."


This means that every double[ n ] is a container, and each container holds 5 apples. The container is not an apple. It is 'a container of one or more apples'. You can visualize it as a paper bag if you like.


apple triple[][][] = new apple[ 5 ][ 5 ][ 5 ];
triple is "an array of 5 arrays of 5 arrays of apples" or perhaps "5 containers of 'containers of apples'".


Going back to your original question, here is the situation: buffer is 'an array of integers'. (I deliberately did not state the size, because buffer can be assigned an array of any size of the same dimensions.) statBuffers is 'an array of arrays of arrays of arrays of integers'. Each element in statBuffers[][][] is itself an 'array of integers', and that is why the assignment works.


I hope that helped. If not, best thing is to just practice with arrays it until it sinks in. -- Martin