This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble creating 2-d array  RSS feed

 
Larry Reynolds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having trouble creating a 2-d array to hold the squares on a board, I am trying to create a class Board that has the member GridSquare[][]. Thanks in advance.

Here is the code that I wrote that won't compile:

public class Board {
int W=2;
int H=3;
gridW gridWidth=new gridW[W];
gridH gridHeight=new gridH[H];
GridSquare[][] squares=new GridSquare[gridWidth][gridHeight];
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are "gridW" and "gridH"? From the way you're using them, they look like names of classes. You have to use a number as an array dimension, not an instance of a class, so that's a big problem right there.
 
Larry Reynolds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,
Thanks, I will fix that and have tried again. Here is the latest code, it still doesn't work:

public class Board {
private int gridWidth=2;
private int gridHeight=3;
GridSquare[][] squares=new GridSquare[gridWidth][gridHeight];
}}
[ November 20, 2005: Message edited by: Larry Reynolds ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you intend this 2-d array to hold? In your code, you're declaring it to hold instances of GridSquare. Have you defined GridSquare as a class?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would also help if we have an idea of what the compiler is complaining about... could you show us the error message?

Henry
 
Larry Reynolds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the help. I hadn't declared GridSquare as a class. This code compiles:

public class GridSquare {
public static void main (String args[]){

int gridWidth=2;
int gridHeight=3;
Object squares [][]=new GridSquare[gridWidth][gridHeight];
}
}
[ November 20, 2005: Message edited by: Larry Reynolds ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that compiles, but I think you still need to go back to the question of what exactly you intend to hold in this array. In this case, what is a GridSquare (other than a class with a main method and some local variables)?

For example, if you want "squares" to hold true/false values, then you would probably want a boolean array...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!