5

A part of my Java assignment requires me to create an enumeration which represents four different types of masks (shapes) which cover squares of a game board. The masks are 3x3 in measurement, with some squares of the 3x3 block missing (these do not hide squares on the game board).

1 0 1  //you can think of the 0's as missing squares, and the 1's as the mask
1 1 1
1 1 0

Now I want to attach a binary matrix to like above to each of the four unique masks using arrays like int[][], like so:

public enum Mask {
    W([[1,1,0],[1,1,1],[1,0,1]]),
    X([[1,0,1],[1,0,1],[1,1,1]]),
    Y([[0,1,1],[1,1,1],[1,1,0]]),
    Z([[1,0,1],[1,1,1],[1,0,1]]);

My IDE becomes very unhappy when I try to do this, so I'm sure I'm missing something in my understanding of enums/arrays.

I'm guessing I can't initialize the array in the enum or something like that?

How do I implement this idea?

1 Answer 1

8

My IDE becomes very unhappy when I try to do this

yes, that is because you are giving the array wrong..

you just cant do this:

W([[1,1,0],[1,1,1],[1,0,1]]),

is not valid and the compiler has no idea that you are "trying to pass" an array as argument, at the end you are just trying to give as parameter an array (in your case an annonymous one), so you have to use a syntax of the form

W(new int[] { 1, 1 });

see this exaplem below as an orientation point:

enum Mask {
    W(new int[] { 1, 1 });
    private final int[] myArray;

    private Mask(int[] myArray) {
        this.myArray = myArray;
    }

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

5 Comments

++ for leaving something for OP to do instead of doing whole homework.
you beat me for a few seconds but I'd like to add for OP: An enum is just a class with the additional ability to declare your instances of it. You can do everything with it that you can do with a class. Declare constructors, fields, methods etc...
Is it possible to instantiate the array outside of the enum parameter and instead just refer to it?
EI_EXPOSE_REP - this bug is coming with spot-bug plugin
To avoid this use the getter as below for the field @Override public String[] getMyArray() { return Arrays.copyOf(myArray, myArray.length); }

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.