Java嵌套循环以检查数组中的元素是否匹配

Object class loop (array1 and array2 are the arrays):

public int howManyMatches(int[] array2){
    int count = 0;

    for (int i = 0; i < array2.length; i++) 
    {
        for (int j = 0; j < array1.length; j++)
        {
            if (array2[i] == (array1[j]))
            {
                count++;   
            } 
        }
    }
    return count;
}

主类检查:

for (int i = 0; i < ticketArray.length; i++)
{
    int count = ticketArray[i].howManyMatches(array2);

    if (count == 4) 
    {
        System.out.println("All 4 elements match");
    }

    else if (count == 3)
    {
        System.out.println("Match 3");
    }

    else
    {
        System.out.println("Arrays do not match.");
    }
}

Loop only returns the else statement, even if the arrays are all matching. array1 is an input based array, and array2 is hardcoded.

评论