Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

2 dimensional array of objects??

Thread view: 
projektwi-05@yahoo.de - 02 Jan 2006 21:30 GMT
Hi,

I want to program a 2 dimensional array of objects. So far I've got
following code snippet (note node is not finished yet just a skeleton):

class node
    {
        int id,u,l,r,d;

    }

public void run () {

    node [][] gamefield  = new node[getWidth()][getHeight()];

    for (int laufx=0; laufx < gamefield.length; laufx++)
 {
 gamefield[laufx] = new node[ gamefield[laufx].length ];
 for (int laufy=0; laufy < gamefield[laufx].length; laufx++)
     {
        gamefield[laufx][laufy] = new node;
     };
 };

}

Java complains:

in_extension.java:38: '(' or '[' expected
                               gamefield[laufx][laufy] = new node;
                                                                 ^
1 error

So whats wrong here? I only want to assign one reference to a node
object nothing else. Googled already, but I found no example or
explanation. All I need is 2 dimensional array containing node
objects..

Any suggestions??

Thanks in advance
VisionSet - 02 Jan 2006 21:40 GMT
> in_extension.java:38: '(' or '[' expected
>                                 gamefield[laufx][laufy] = new node;
>                                                                   ^
> 1 error
>
> So whats wrong here?

gamefield[laufx][laufy] = new node();

Class names begin with uppercase letters, please fix this!

--
Mike W
Mark Thomas - 02 Jan 2006 22:16 GMT
> Hi,
>
[quoted text clipped - 10 lines]
>
>     node [][] gamefield  = new node[getWidth()][getHeight()];

at this point you have your two dimensional array of nodes

>     for (int laufx=0; laufx < gamefield.length; laufx++)

Here gamefield.length will give you the size of the first dimension of
your array (from getWidth() above). If you need it, gamefield[0].length
will give the other dimension.

>   {
>   gamefield[laufx] = new node[ gamefield[laufx].length ];

Whoops!  Here you are creating another single dimension array (which I
don't think you need, and then trying to assign it to a cell in your
original array - but to do that you would need to specify both indices -
something like gamefield[x,y].

>   for (int laufy=0; laufy < gamefield[laufx].length; laufx++)
>      {
>         gamefield[laufx][laufy] = new node;

new node(); to call node's constructor.  Note that convention has it
that class names should start with a capital letter, so Node would be a
better name than node.

>      };
>   };
[quoted text clipped - 14 lines]
>
> Any suggestions??

node [][] gamefield  = new node[getWidth()][getHeight()];
for (int laufx=0; laufx < gamefield.length; laufx++)
{
  for (int laufy=0; laufy < gamefield[laufx].length; laufx++)
  {
    gamefield[laufx][laufy] = new node();
  }
}

would create your array and fill it with default nodes.

Mark
Roedy Green - 03 Jan 2006 00:38 GMT
>I want to program a 2 dimensional array of objects. So far I've got
>following code snippet (note node is not finished yet just a skeleton):

see http://mindprod.com/jgloss/gotchas.html#MATRIX
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.