|
-
Dec 25th, 2002, 03:26 PM
#1
Initializing arrays.
I have a class which contains a 2-d boolean array as private member. The class also contains a static array of 7 instances of the class, the only instances that will ever exist. And I have a static block to create that array:
Code:
class TetrisBlock
{
private boolean[][] blockLook;
private TetrisBlock()
{
}
private static TetrisBlock[] blocks;
static {
blocks = new TetrisBlock[7];
blocks[0] = new TetrisBlock();
blocks[0].blockLook = { { true, true, true, true}, // ####
{false, false, false, false} }; // ----
blocks[1] = new TetrisBlock();
blocks[1].blockLook = { { true, true, true, false}, // ###-
{ true, false, false, false} }; // #---
blocks[2] = new TetrisBlock();
blocks[2].blockLook = { { true, false, false, false}, // #---
{ true, true, true, false} }; // ###-
blocks[3] = new TetrisBlock();
blocks[3].blockLook = { {false, true, true, false}, // -##-
{ true, true, false, false} }; // ##--
blocks[4] = new TetrisBlock();
blocks[4].blockLook = { { true, true, false, false}, // ##--
{false, true, true, false} }; // -##-
blocks[5] = new TetrisBlock();
blocks[5].blockLook = { {false, true, false, false}, // -#--
{ true, true, true, false} }; // ###-
blocks[6] = new TetrisBlock();
blocks[6].blockLook = { { true, true, false, false}, // ##--
{ true, true, false, false} }; // ##--
}
The compiler says this code is not correct. It gives me the following error:
TetrisBlock.java:17: illegal start of expression
blocks[0].blockLook = { { true, true, true, true}, // ####
It gives me the same error for all similar lines.
What am I doing wrong? Isn't this the way to directly initialize arrays?
Thanks in advance.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 25th, 2002, 11:49 PM
#2
Addicted Member
I think you need to initialize while you declare, or else you need a set method to assign the initial values. This compiles:
Code:
class TetrisBlock
{
public static boolean[][] get(int aBlockLook){
return blockLook[aBlockLook];//aBlockLook between 0 and 6
}
private TetrisBlock(){
}
private static boolean[][][] blockLook = {
{ { true, true, true, true}, // ####
{false, false, false, false} },
{ { true, true, true, false}, // ###-
{ true, false, false, false} },
{ { true, false, false, false}, // #---
{ true, true, true, false} },
{ {false, true, true, false}, // -##-
{ true, true, false, false} },
{ { true, true, false, false}, // ##--
{false, true, true, false} },
{ {false, true, false, false}, // -#--
{ true, true, true, false} },
{ { true, true, false, false}, // ##--
{ true, true, false, false} }
};
}
It should be addressable as blockLook[0] through blockLook[6] within the class. But you might want a 2D array getter like I added.
Merry Christ-mas
-
Dec 26th, 2002, 10:41 AM
#3
Strange, I was quite sure this should work by the language definition.
Thanks for your suggestions though. I'll remember it for the future, I have already solved my current problem simply by typing more
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 1st, 2003, 10:26 PM
#4
Dazed Member
I dont think the problem is with the actual initialization of the array. The way Phenix has pointed out to me would be the same as using a static{} initializer. When the class loads all initialization expressions and initializer blocks should be executed in the order that they were declared.
Code:
class A{
public static void main(String[] args){
B b = new B();
B.printValues();
}
}
class B{
private static String[] s = new String[5];
static {
s[0] = "One";
s[1] = "Two";
s[2] = "Three";
s[3] = "Four";
s[4] = "Five";
}
public static void printValues(){
for(int i = 0; i < s.length; i++){
System.out.println(s[i]);
}
}
}
Last edited by Dilenger4; Jan 1st, 2003 at 10:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|