简体   繁体   English

Java矩阵包含ArrayList元素

[英]Java matrix contains ArrayList elements

I am writing a tictactoe game, I have 1 ArrayList<String> OFields (it's containing fields that O is owning like TOP_LEFT, CENTRE_RIGHT etc) and a 我正在写一个ticticactoe游戏,我有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"}};

I want to check, that OFields's elements contains winComb[1..8] 我想检查一下,OFields的元素是否包含winComb [1..8]

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

That's what i have, it's working for the first winComb element but it would be really messy if i write like this. 那就是我所拥有的,它适用于第一个winComb元素,但是如果我这样写的话,那真是一团糟。 How can i check that all OFields element is in winComb matrix? 如何检查所有OFields元素是否在winComb矩阵中?

You can use List#containsAll() : 您可以使用List#containsAll()

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

But sincerely saying, I agree with @Jägermeister that you should chose more appropriate class model for that. 但是真诚地说,我同意@Jägermeister你应该为此选择更合适的班级模型。

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

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