char[] vowels = {'a', 'e', 'i', 'o', 'u'};
String aWord = "any word";
if(aWord.charAt(0) == 'a' || aWord.charAt(0) == 'e' ) {
//As you can see, this will be very messy by the time I get round to the last vowel.
}
if(aWord.charAt(0) == vowels) {
//This is illegal, is there a way to accomplish what I'm trying to get at?
}
在上面的代码中不言自明。任何帮助不胜感激,谢谢!
对于数组 no,您没有直接的方法,但有 Collection.contains()
是。
例如,Set
:
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
//...
if(vowels.contains(aWord.charAt(0)) {
// ...
}