Re: Array of something
Merciadri Luca wrote:
I am defining a `Tableau' array of `Cell' elements. Tableau and Cell
Show us some code. I don't know what you mean by "a `Tableau' array of `Cell'
elements".
are both classes, and some methods over `Tableau' and `Cell' are
defined.
Now, my Cell class comprises a State so that to each Cell
instanciation a State is associated, i.e. one can make
==
Cell myCell = new Cell();
myCell.state
What do you expect to happen as a result of that standalone expression?
==
Now, for the `Tableau' array, if I need to access a Cell's state, do I need to write, for a `Tableau'
element: myTableau.cell.state? It would mean that a `cell' (of type
Cell) is defined for each `Tableau' element, and that is not the
case. But using myTableau.state would mean that `state' is a variable of
myTableau (of type `Tableau'), which is not the case too, because
state is defined for each Cell element.
Remember that a reference variable can have a value of 'null', and member
variables always start at their "zero" value, which for references is 'null'.
Whatever you do to get a 'Cell', it's clearly 'Cell#state' that you want.
Show us class definitions. You're not speaking Java so it's really, really
hard to figure out what you need help with.
You can have something like this:
package tableaux;
public class Tableau
{
/* p-p */ Cell [] cells;
...
}
and
package tableaux;
public class Cell
{
/* p-p */ State state;
...
}
Your code to access individual 'Cell' state would be similar to:
package tableaux;
import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
public class Processor
{
/* p-p */ Tableau tableau;
// various setter methods that should give 'tableau' a value
public void process()
{
if ( tableau == null || tableau.cells == null )
{
return;
}
for ( Cell cell : tableau.cells )
{
if ( cell != null && cell.state != null )
{
doSomethingWith( cell.state );
}
}
}
}
I'm ignoring the convention and best practice to use accessor and mutator
(getter and setter) methods instead of raw access to fields. I did not ignore
variable accessibility; I deliberately chose package-private
('/* p-p */') access.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg