java 是否可以检查字符是否与可能性列表匹配?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27555895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:59:09  来源:igfitidea点击:

is it possible to check if a char matches a list of possibilities?

javaif-statementchar

提问by Manuel Buenrostro

For example I know that when checking strings, you can do something like

例如,我知道在检查字符串时,您可以执行以下操作

if (string.matches("a|e|i|o|u|A|E|I|O|U" ) )
{
   // Then do this code.
}

but is there a way to check if a char matches a list of possibilities? or do I have to check one by one, such as

但是有没有办法检查字符是否与可能性列表匹配?还是我要一一检查,比如

if(char == a || char == e || char == i )

...ect.

...等等。

回答by rgettman

You can do something similar when looking for a charin a String, by using the indexOfmethodto search the string.

char在 a 中查找 a 时,您可以执行类似String的操作,indexOf方法是使用搜索字符串的方法

if ("aeiouAEIOU".indexOf(aChar) != -1)

回答by Marko Topolnik

From the performance standpoint the optimum approach would be this:

从性能的角度来看,最佳方法是这样的:

private final BitSet matchChars = matchChars();

private BitSet matchChars() {
  final BitSet bs = new BitSet();
  final String matchString = "aeiouAEIOU";
  for (int i = 0; i < matchString.length(); i++)
     bs.set(matchString.charAt(i));
  return bs;
}

public boolean charMatches(char c) { return matchChars.get(c); }

Memory required for the approach is very modest even if you use the whole 16-bit range afforded by the chartype: at most 8 KB.

即使您使用该char类型提供的整个 16 位范围,该方法所需的内存也非常适中:最多 8 KB。

回答by Mshnik

You could make a collection of chars that you want to check, and see if the collection contains the char in question. A HashSet is ideal here for O(1) look up time. (not that it matters, because the size is constant.)

您可以制作要检查的字符集合,并查看该集合是否包含相关字符。HashSet 是 O(1) 查找时间的理想选择。(并不重要,因为大小是恒定的。)

private static final HashSet<Character> vowels = new HashSet<Character>();

//Initialize vowels hashSet to contain vowel characters
static{
    vowels.add('a');
    vowels.add('e');
    vowels.add('i');
    vowels.add('o');
    vowels.add('u');
    vowels.add('A');
    vowels.add('E');
    vowels.add('I');
    vowels.add('O');
    vowels.add('U');
}

public static boolean isVowel(Character c){
    return vowels.contains(c);
}

回答by ajb

Back before we had Unicode, when the character set was only 128 characters (ASCII) or later 256 (ISO 8859-1), we would often just create an array of Booleans and use a lookup on the character code--very fast. You could still do the same (an array of 65536 booleans isn't all that big by today's memory standards), or something like

在我们有 Unicode 之前,当字符集只有 128 个字符 (ASCII) 或后来的 256 个字符 (ISO 8859-1) 时,我们通常只会创建一个布尔数组并使用字符代码查找——非常快。你仍然可以做同样的事情(按照boolean今天的内存标准,65536的数组并不是那么大),或者类似的

static boolean[] vowelSet = new boolean[128]; // all initialized to false by default

static {
    vowelSet['A'] = true;
    vowelSet['E'] = true;
    ...
    vowelSet['u'] = true;
}

and then to look up:

然后查找:

boolean isVowel(char ch) {
    return ch < 128 && vowelSet[ch];
}

I think that's still the approach I'd take if efficiency were extremely important. Usually it isn't, so one of the other answers probably gives you more readable code.

如果效率非常重要,我认为这仍然是我会采取的方法。通常不是,所以其他答案之一可能会为您提供更具可读性的代码。