1
public class Test {
    boolean[][] expectedResults = new boolean[8][2];
    expectedResults[0] = new boolean[] {false, false}; //00B
}       

Gives me the following Compile Time error:

test.java:3: ']' expected
expectedResults[0] = new boolean[] {false, false}; //00B
^
test.java:3: ';' expected
expectedResults[0] = new boolean[] {false, false}; //00B
^
test.java:3: illegal start of type expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: expected expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: ';' expected expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: illegal start of type expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: expected expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: ';' expected expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: illegal start of type expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: expected expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:3: ';' expected expectedResults[0] = new boolean[] {false, false}; //00B
^ test.java:4: class, interface, or enum expected
}

Solved

Must be in a method!.

1
  • You haven't posted your actual code, because it doesn't match the error message. Commented Apr 4, 2012 at 9:59

4 Answers 4

5

The syntax is:

expectedResults[0] = new boolean[]{false, false};

Also, make sure you perform the assignment inside a method or static initialization block.

Sign up to request clarification or add additional context in comments.

1 Comment

See the second part of my answer. it should be in a method, not just in the class.
0

You can creating an array like this {false, false}, only when you put it in the same line with the decleration (Array constants can only be used in initializers):

boolean[] expectedResults = {false, false}; // complies

While this does not compile:

boolean[] expectedResults;
expectedResults = {false, false}; // does not complie

instead use:

boolean[] expectedResults;
expectedResults = new boolean[]{false, false};

Comments

0

This is 8x2 boolean array initialization made easy.

  public boolean[][] expectedResults = {
    {false, false},
    {false, false},
    {false, false},
    {false, false},
    {false, false},
    {false, false},
    {false, false},
    {false, false},
  };

This is a 4x8 boolean array initalization made easy.

  public boolean[][] expectedResults = {
    {false, false, false, false, false, false, false, true},
    {true, false, false, false, false, false, false, true},
    {true, false, false, false, false, true, true, true},
    {false, true, true, true, true, true, true, false}
  };

Comments

-2

I think you have to initialize the array like this.

boolean[][] expectedResults;
 expectedResults = new boolean[8][2];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.