Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Writing values to a two dimensional array

I've been driving my instructor crazy and thought I would try you guys for awhile. I can not comprehend how to pass values to my 2D array, "theSimulation". I'm only supplying the code for the class that is affected by this-the error is occuring at the very bottom and i've tried everything I can think of but to no avail--any help or advice is greatly appreciated

public class Colony
{

/*************
* attributes
************/

/** the 2-D array of ColonyNode objects */
private static ColonyNode[][] theColony;

/** colony height, in ColonyNodes */
private int height;

/** colony width, in ColonyNodes */
private int width;

/** the view used by this Colony */
private ColonyView colonyView;

/** create a 2D array of ColonyNode objects */
private ColonyNode theSimulation[][] = new ColonyNode[27][27]; //creates 2D array

private int x;
private int y;

/**
/***************
* constructors
**************/


/**
* Create a new Colony.
*
* @param colonyHeight: height of Colony, measured in ColonyNodes
* @param colonyWidth: width of Colony, measured in ColonyNodes
* @param view: the ColonyView this Colony will use
*/
public Colony(int colonyHeight, int colonyWidth, ColonyView view)
{
// set height and width
height = colonyHeight;
width = colonyWidth;

// set view
colonyView = view;

// create the ColonyNode objects comprising this Colony
buildColony();
}


/**********
* methods
*********/


/**
* Build the colony.
*/
private void buildColony()
{
// stores current ColonyNode
ColonyNode currentNode;

// stores ColonyNodeView for current ColonyNode
ColonyNodeView currentNodeView;

// create nodes
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
// create next node
currentNode = new ColonyNode(x,y,0,0,0,0,0,0);

// create a ColonyNode View for the current ColonyNode
currentNodeView = new ColonyNodeView();

// give current ColonyNode a reference to its ColonyNodeView
currentNode.setView(currentNodeView);

// add the current ColonyNodeView to the Colonyview
colonyView.addColonyNodeView(currentNodeView, x, y);

//add the current NodeView to the Simulation Array
addColonyView(currentNode, x, y);

//create relationship for Simulation class
//Simulation.addSimulationColonyNodeView(x, y);

// this is only here to prove the colony has been created;
// DO NOT make any nodes visible here
//****Move this to Simulation class
//currentNodeView.showNode();
//initialize colony to show only the start-up nodes
if
(x >= 12 && x <=14 && y >= 12 && y <=14)
currentNodeView.showNode();
else
currentNodeView.hideNode();

} //end nested for loop
} //end for
} //end buildColony


public void addColonyNode(ColonyNode node, int x, int y)
{
//theSimulation[][]= x,y;(this code commented out)
theSimulation[][]= currentNode; <<<<<<<<<<<<<ERROR HERE
}// end addColonyNode method
}
[4117 byte] By [JavaBeanie] at [2007-11-11 7:21:07]
# 1 Re: Writing values to a two dimensional array
its pretty messy on the eyes without the forum CODE tags, but from a quick glimpse, it seems like theSimulation[][] = currentNode generates an error because currentNode is not within the scope of addColonyNode so it can't really find that variable...you've declared currentNode inside the buildColony method. You could declare it as a "global" variable and only instantiate it inside buildColony for addColonyNode to have access to it.
chimps at 2007-11-11 22:38:38 >