Is there a simpler way to check multiple values against one value in an if-statement?

前端 未结 12 864
名媛妹妹
名媛妹妹 2020-11-27 07:55

Basically, what I want to do is check two integers against a given value, therefore, classically what you would do is something like this:

//just to get some         


        
12条回答
  •  醉梦人生
    2020-11-27 07:57

    You can try this code:

    public static boolean match (Object ref, Object... objects)
    {
        if (ref == null)
            return false;
        //
        for (Object obj : objects)
            if (obj.equals (ref))
                return true;
        //
        return false;   
    }   //  match
    

    So if you can check this way:

    if (match (reference, "123", "124", "125"))
        ;   //  do something
    

提交回复
热议问题