Re: Array of something
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lew <noone@lewscanon.com> writes:
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.
Thanks for your help. I will try to be more explicit in this message.
Here is a more complete description.
* I've got a Cell class which should allow me to define a state State for
every Cell instanciation. That is, I define
==
public class Cell
{
public enum State
{
DEAD, LIVING
}
public State state;
}
==
(these are either DEAD or LIVING `Cell' that will be instanciated).
* I want to compare the value of the state of a Cell with a State
`constant,' i.e. do something like
==
if (tableau.setOfCells[i][j].state == State.LIVING)
{
....
}
==
but compiler complains: `cannot find symbol State.'
* In the last chunk of code, tableau is a Tableau element where I've
got a class `Tableau' defined like this:
==
public class Tableau
{
public Cell[][] setOfCells;
public Tableau(int n, int s)
{
setOfCells = new Cell[n][n];
}
}
==
and where tableau is created thanks to the call to the accessor:
==
Tableau tableau = new Tableau(n, s);
==
That is, my main goal is to create two instances of `Tableau': tableau
and tableau2, which should contain each one a `setOfCells' element,
which would be a n*n Cell array. This array would then be accessed
using habitual indices, and, an element of this Cell array being a
Cell, accessing a Cell would lead to the possibility of accessing its state.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --
If you want a thing done right, do it yourself.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iEYEARECAAYFAk2M+eUACgkQM0LLzLt8MhwFAACePCVCnYM3TcthxfgDFrZYAHBD
3MgAoK1bN3pMW97m3zv1a7/bO3X8fJlZ
=Nrko
-----END PGP SIGNATURE-----