简体   繁体   English

如何在枚举中创建多个二维数组?

[英]How do I create multiple two dimensional arrays in an enum?

I am sort of new to java and I am currently attempting to make a small Naughts and Crosses program complete with "ai". 我是Java的新手,并且目前正在尝试制作一个完整的,包含“ ai”的小程序。

I decided to let the computer make it's move by comparing the current 2d char array to an enum of 2d arrays that have been set up with multiple scenarios and then to move accordingly. 我决定通过将当前的2d char数组与已为多种场景设置的2d数组的枚举进行比较,然后让其进行相应移动来让计算机移动。 The only problem at the moment is that I am unsure as to how to make a 2d char array inside an enum with specific values. 目前唯一的问题是我不确定如何在具有特定值的枚举内创建2d char数组。 If anyone could specify as to how I would accomplish this that would be great, or maybe I'm going about the problem the wrong way? 如果有人可以指定我将如何完成此任务,那将是很棒的,或者我可能会以错误的方式解决问题?

Any help regarding the matter would be highly valued, thank-you. 谢谢您对这个问题的任何帮助。

A possibility is to "linearise" the data: 一种可能是“线性化”数据:

public enum Foo {
    BAR("1010"), LOL("0101");

    private final String data;

    private Foo(final String data) {
        this.data = data;
    }

    public char[][] get2DArray() {
        // construct from data  
    }
}

With a transformation like: 通过类似的转换:

1 0
0 1   =>  1001

Enums can be thought of as classes where you have a static reference to named instances of that class. 枚举可被视为类,您可以在其中静态引用该类的命名实例。

Thinking this way leads you to the idea of declaring the static 2d array in the enum the same way you would any other static data in any class. 以这种方式思考会使您想到在枚举中声明静态2d数组的方法,就像在任何类中声明任何其他静态数据一样。

public enum MyEnum {

Type1,Type2;

private static int [][] _2dArr = new int[10][10];
static {
    _2dArr[0][0] = 1;
}
}

Basically the same way you would do any class. 基本上,您上任何课的方式都相同。 Not sure what you want exactly but here is an example. 不确定您到底想要什么,但是这里有一个例子。

enum Scenario {
    ONE(new char[] {
        { 'X', ' ', ' ' },
        { ' ', ' ', ' ' },
        { 'X', ' ', ' ' }
    }),
    TWO(new char[] {
        { ' ', ' ', ' ' },
        { ' ', 'X', 'X' },
        { ' ', ' ', ' ' }
    });

    private final char[] board;

    private Scenario(char[] board) {
        this.board = board;
    }

    public boolean matches(char[] gameBoard) {
        for(int i = 0; i < board.length; i++) {
            for(int k = 0; k < board[i].length; k++) {
                if(board[i][k] != gameBoard[i][k])
                    return false;
            }
        }

        return true;
    }
}

Enum is just a class and the constants are objects of that class. 枚举只是一个类,常量是该类的对象。 So you can make methods, constructors, etc. The syntax is just a little bit different. 因此,您可以制作方法,构造函数等。语法略有不同。

you might try something like this 你可以尝试这样的事情

    public enum ArrayCheck {
        Array1  (new int[][]{
        {1 , 0, 1},
        {1 , 0, 1},
        {1 , 0, 0}
    };),

        Array2(new int[][]{
        {1 , 0, 1},
        {1 , 0, 1},
        {1 , 0, 0}
    };)

        int[][] matrix;
        ArrayCheck(int[][] matrix) {
            this.matrix = matrix;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM