Java矩阵包含ArrayList元素

时间:2016-03-22 15:20:14

标签: java matrix arraylist

我正在写一个tictactoe游戏,我有1 ArrayList<String> OFields(它包含O拥有的字段,如TOP_LEFT,CENTRE_RIGHT等)和

String[][] winComb={{"TOP_LEFT","TOP_CENTRE","TOP_RIGHT"}, {"CENTRE_LEFT","CENTRE_CENTRE","CENTRE_RIGHT"},
                    {"BOT_LEFT","BOT_CENTRE","BOT_RIGHT"}, {"TOP_LEFT","CENTRE_LEFT","BOT_LEFT"}, 
                    {"TOP_CENTRE","CENTRE_CENTRE","BOT_CENTRE"}, {"TOP_RIGHT","CENTRE_RIGHT","BOT_RIGHT"},
                    {"TOP_LEFT","CENTRE_CENTRE","BOT_RIGHT"}, {"TOP_RIGHT","CENTRE_CENTRE","BOT_LEFT"}};

我想检查一下,OFields的元素包含winComb [1..8]

if(OFields.contains((winComb[0][0])) && OFields.contains(winComb[0][1]) && OFields.contains(winComb[0][2])){
                                        System.out.println("win");
                                    }

这就是我所拥有的,它适用于第一个winComb元素但如果我这样写的话会非常混乱。如何检查所有OFields元素是否在winComb矩阵中?

1 个答案:

答案 0 :(得分:0)

您可以使用List#containsAll()

static boolean win(List<String> oFields) {
    for (String[] positions : WIN_COMB) {
        if (oFields.containsAll(Arrays.asList(positions)))
            return true;
    }
    return false;
}

但是真诚地说,我同意@Jägermeister你应该为此选择更合适的课堂模式。