In Java, how can I determine if a char array contains a particular character?

后端 未结 7 1620
野趣味
野趣味 2020-12-05 18:53

Here\'s what I have:

char[] charArray = new char[] {\'h\',\'e\',\'l\',\'l\',\'o\'};

I want to write something to the effect of:

<         


        
7条回答
  •  [愿得一人]
    2020-12-05 19:34

    From NumberKeyListener source code. This method they use to check if char is contained in defined array of accepted characters:

    protected static boolean ok(char[] accept, char c) {
        for (int i = accept.length - 1; i >= 0; i--) {
            if (accept[i] == c) {
                return true;
            }
        }
    
        return false;
    }
    

    It is similar to @ÓscarLópez solution. Might be a bit faster cause of absence of foreach iterator.

提交回复
热议问题