Array of something

  • Follow


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I am defining a `Tableau' array of `Cell' elements. Tableau and Cell
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
==

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.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

If you fake it, you can't make it.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2M2x4ACgkQM0LLzLt8Mhz9FACfRcpaVQa3pjpEca+AL/hxpl97
3u0An3Q21V8/SuO3t9LLFPNs2uP3eMhz
=hxFD
-----END PGP SIGNATURE-----
0
Reply Merciadri 3/25/2011 6:12:47 PM

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
0
Reply Lew 3/25/2011 7:17:37 PM


-----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-----
0
Reply Merciadri 3/25/2011 8:24:06 PM

On Fri, 25 Mar 2011 19:12:47 +0100, Merciadri Luca
<Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
someone who said :

>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.

You need a intro textbook to answer such questions.  It can be out of
date and hence free or very cheap.  See
http://mindprod.com/jgloss/gettingstarted.html

See also http://mindprod.com/jgloss/array.html

You normally would not create a class just to hold a single array.

You would say:

Cell[] cells = new Cell[ xxx ];

cells[ 0 ] = new Cell( yyy );

cells[ 0 ].someMethod();

or, more commonly, since you usually don't know in advance how many
Cells there will be:

List<Cell> cells = new ArrayList<Cell>( xxx );

cells.add ( new Cell ( yyy ) );

Cell aCell = cells.get( 0 );

aCell.someMethod();


-- 
Roedy Green Canadian Mind Products
http://mindprod.com
If you think it’s expensive to hire a professional to do the job, wait until you hire an amateur.
~ Red Adair

0
Reply Roedy 3/25/2011 8:49:45 PM

Merciadri Luca wrote:
> 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.'

That's because 'State' is a nested class - it has to be accessed through its 
outer class.  'State' is a static member of 'Cell', not a standalone class.

Try 'Cell.State.LIVING', or else elevate 'State' to a top-level class:

------------------------------------
  package tableaux;
  public enum State
  {
    DEAD, LIVING
  }
------------------------------------
------------------------------------
  package tableaux;
  public class Cell
  {
   public State state;
  }
------------------------------------

Remember that public fields of a type are an antipattern in Java.

> * 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;

Bad name for something that is not a 'Set'.

>     public Tableau(int n, int s)
>     {
>         setOfCells = new Cell[n][n];
>     }
> }
> ==

What does the constructor 's' argument contribute?

> 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

"habitual"?

> Cell, accessing a Cell would lead to the possibility of accessing its state.

OK

But what you show is an n x n array of 'null' references, so you have nothing 
whose state you can access.

  public class Tableau
  {
   /* p-p */ Cell[][] cells;

   public Tableau(int n, int s)
   {
     cells = new Cell[n][n];
     for ( int ix = 0; ix < n; ++ix )
     for ( int jx = 0; jx < n; ++ix )
     {
       cells [ix] [jx] = getValue( ix, jx );
     }
   }

   public void doSomething()
   {
     for ( int ix = 0; ix < cells.length; ++ix )
     for ( int jx = 0; ix < cells [0].length; ++ix )
     {
       final Cell cell = cells [ix] [jx];
       if ( cell != null )
       {
         process( cell.state );
       }
     }
   }
  }


If you don't handle error conditions, preferably via prevention, your program 
will crash horribly and embarrass you beyond belief.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
0
Reply Lew 3/25/2011 10:44:37 PM

Roedy Green wrote:
> You normally would not create a class just to hold a single array.

Other than the trivial fact that no array exists outside a class (or at least 
as a static member of an interface and that would be sick).

The key word is "just".  You might indeed create a class that contains a 
single array, but it would have behaviors, too.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
0
Reply Lew 3/25/2011 10:48:30 PM

On Fri, 25 Mar 2011 18:48:30 -0400, Lew <noone@lewscanon.com> wrote,
quoted or indirectly quoted someone who said :

>The key word is "just".  You might indeed create a class that contains a 
>single array, but it would have behaviors, too.

If you did create a class containing just an array and a few methods,
you would almost never make the array itself public.  You would
manipulate it via getter and setter method of the class.

So in practice you almost always access an array from within the class
in which it is defined.

I remember once being given a test.  Many of the question were about
the details of the COBOL IDENTIFICATION division.  I thought to
myself, anyone who knew this stuff was probably a loon who wrote it
out from scratch each time.  KNOWING this was in indication of
incompetence.

In a way, knowing how to access arrays from outside the class, at
least in one's sleep, is contraindicative of competence.  It is not
something you should do regularly.  On the other hand, any old hand
could reason it out from first principles. It depends on whether the
array itself is static or instance.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
If you think it’s expensive to hire a professional to do the job, wait until you hire an amateur.
~ Red Adair

0
Reply Roedy 3/25/2011 11:07:06 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew <noone@lewscanon.com> writes:

> Merciadri Luca wrote:
>> 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.'
>
> That's because 'State' is a nested class - it has to be accessed
> through its outer class.  'State' is a static member of 'Cell', not a
> standalone class.
Thanks. This is part of the way I solved the problem.

> Try 'Cell.State.LIVING', or else elevate 'State' to a top-level class:
>
> ------------------------------------
>  package tableaux;
>  public enum State
>  {
>    DEAD, LIVING
>  }
> ------------------------------------
> ------------------------------------
>  package tableaux;
>  public class Cell
>  {
>   public State state;
>  }
> ------------------------------------
>
> Remember that public fields of a type are an antipattern in Java.
>
>> * 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;
>
> Bad name for something that is not a 'Set'.
>
>>     public Tableau(int n, int s)
>>     {
>>         setOfCells = new Cell[n][n];
>>     }
>> }
>> ==
>
> What does the constructor 's' argument contribute?
It contributed for another affectation in the constructor body. I
would have better put // ... so that you would have realized that the
's' argument was necessary.
>
>> 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
>
> "habitual"?
Yes, say now tableau.setOfCells[x][y]. `habitual' -> generic way.
>
>> Cell, accessing a Cell would lead to the possibility of accessing its state.
>
> OK
>
> But what you show is an n x n array of 'null' references, so you have
> nothing whose state you can access.
>
>  public class Tableau
>  {
>   /* p-p */ Cell[][] cells;
>
>   public Tableau(int n, int s)
>   {
>     cells = new Cell[n][n];
>     for ( int ix = 0; ix < n; ++ix )
>     for ( int jx = 0; jx < n; ++ix )
>     {
>       cells [ix] [jx] = getValue( ix, jx );
>     }
>   }
>
>   public void doSomething()
>   {
>     for ( int ix = 0; ix < cells.length; ++ix )
>     for ( int jx = 0; ix < cells [0].length; ++ix )
>     {
>       final Cell cell = cells [ix] [jx];
>       if ( cell != null )
>       {
>         process( cell.state );
>       }
>     }
>   }
>  }
>
> If you don't handle error conditions, preferably via prevention, your
> program will crash horribly and embarrass you beyond belief.
But why is that necessary? This might exactly be part of the solution
to my other thread, namely `Enum list.'
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Life is like a box of chocolate, you never know what you're gonna
  get.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2OX1cACgkQM0LLzLt8MhxuVwCdHWnJfyQOdw12fPwydIA1uyLq
Qs4An2fA8HTljBqZU4U/LLVYekK9X7e9
=uGwM
-----END PGP SIGNATURE-----
0
Reply Merciadri 3/26/2011 9:49:11 PM

On Fri, 25 Mar 2011 19:12:47 +0100, Merciadri Luca
<Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
someone who said :

>I am defining a `Tableau' array of `Cell' elements. Tableau and Cell
>are both classes, and some methods over `Tableau' and `Cell' are
>defined.

I suggest you have a look at
http://mindprod.com/jgloss/newsgroups.html

For some hints on how to ask questions in a way that elicits useful
responses rather than a shower of virtual vegetables.  It is quite an
art.  If you study past exchanges, you will see it is one I have far
from mastered myself. You can't avoid it entirely, but you can reduce
the number of mass peltings.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
There are only two industries that refer to their customers as "users".
~ Edward Tufte

0
Reply Roedy 3/27/2011 6:17:07 AM

8 Replies
266 Views

(page loaded in 0.116 seconds)

Similiar Articles:













7/26/2012 3:04:00 AM


Reply: