在二维数组中搜索特定行

时间:2018-05-04 05:06:20

标签: java arrays

我正在尝试在二维数组中找到特定的第n行:

    public static String searchNum(int[][] playerArr, int[]winNum, int playerId) {
    // Take in an ID (row) and based on that, check the numbers against the winning numbers and display which category they fall.

    for(int i = 0; i < playerArr.length; i++) { // Loop over the rows (ID's)

        if(playerArr[i] == playerId)){ // If current row = input (player id)
            int currID = i;
            int counter = 0;

            for (int j = 0; j < playerArr[i].length; j++) { // i = row
                int currLottNum = winNum[j];

                boolean contains = IntStream.of(playerArr[i]).anyMatch(x -> x == currLottNum);
                if(contains) {
                    counter ++;
                }
            }

            // Add to array depending on number of matches
            switch(counter) {
                case 6 : winners.add(currID);
                    break;
                case 5 : fifthPlace.add(currID);
                    break;
                case 4 : fourthPlace.add(currID);
                    break;
                case 3 : thirdPlace.add(currID);
                    break;
                default : losers.add(currID);
            }
        }
    }

    return strRes;
}

我正在尝试将2D数组(playerArr [i])的行与给定的int进行比较,但它不会让我“运算符'=='无法应用于int []”

感谢任何帮助。

亲切的问候,

1 个答案:

答案 0 :(得分:0)

我可以看到 playerArr 是一个2D数组,而 playerArr [i] 将返回一维数组。所以你无法比较两者。 现在,如果你想比较每个玩家ID,那么你的代码应该是这样的:

public static String searchNum(int[][] playerArr, int[] winNum, int playerId) {
    // Take in an ID (row) and based on that, check the numbers against the winning numbers and display which category they fall.

    for (int row = 0; row < playerArr.length; row++) { // Loop over the rows (ID's)

        for (int column = 0; column < playerArr[row].length; column++) { // Loop over columns
            if (playerArr[row][column] == playerId) { // If current row = input (player id)
                // your logic
            }

        }
    }
    return strRes;

}