Java – is it possible to find out whether the value exists twice in ArrayList?

I have an integer ArrayList

ArrayList <Integer> portList = new ArrayList();

I need to check whether I have entered a specific integer twice Is this possible in Java?

Solution

I know this is an old problem, but since I am looking for the answer here, I think I will share my solution

public static boolean moreThanOnce(ArrayList<Integer> list,int searched) 
{
    int numCount = 0;

    for (int thisNum : list) {
        if (thisNum == searched) numCount++;
    }

    return numCount > 1;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>