在 Java 中,如何确定 char 数组是否包含特定字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18581531/
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
In Java, how can I determine if a char array contains a particular character?
提问by Aei
Here's what I have:
这是我所拥有的:
char[] charArray = new char[] {'h','e','l','l','o'};
I want to write something to the effect of:
我想写一些东西的效果:
if(!charArray contains 'q'){
break;
}
I realize that .contains() can't be used here. I am just using "contains" to illustrate what I'm trying to do.
我意识到 .contains() 不能在这里使用。我只是使用“包含”来说明我正在尝试做的事情。
回答by óscar López
The following snippets test for the "not contains" condition, as exemplified in the sample pseudocode in the question. For a direct solution with explicit looping, do this:
以下片段测试“不包含”条件,如问题中的示例伪代码所示。对于显式循环的直接解决方案,请执行以下操作:
boolean contains = false;
for (char c : charArray) {
if (c == 'q') {
contains = true;
break;
}
}
if (!contains) {
// do something
}
Another alternative, using the fact that String
provides a contains()
method:
另一种选择,使用String
提供了一种contains()
方法的事实:
if (!(new String(charArray).contains("q"))) {
// do something
}
Yet another option, this time using indexOf()
:
另一个选择,这次使用indexOf()
:
if (new String(charArray).indexOf('q') == -1) {
// do something
}
回答by Ted Hopp
You can iterate through the array or you can convert it to a String
and use indexOf
.
您可以遍历数组,也可以将其转换为 aString
并使用indexOf
.
if (new String(charArray).indexOf('q') < 0) {
break;
}
Creating a new String
is a bit wasteful, but it's probably the tersest code. You can also write a method to imitate the effect without incurring the overhead.
创建新String
代码有点浪费,但它可能是最简洁的代码。您还可以编写一个方法来模拟效果,而不会产生开销。
回答by Ryan
Here's a variation of Oscar's first version that doesn't use a for-each loop.
这是 Oscar 的第一个版本的变体,它不使用 for-each 循环。
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] == 'q') {
// do something
break;
}
}
You could have a boolean variable that gets set to false before the loop, then make "do something" set the variable to true, which you could test for after the loop. The loop could also be wrapped in a function call then just use 'return true' instead of the break, and add a 'return false' statement after the for loop.
您可以将一个布尔变量在循环之前设置为 false,然后让“做某事”将变量设置为 true,您可以在循环之后对其进行测试。循环也可以包含在函数调用中,然后只使用“return true”而不是 break,并在 for 循环后添加“return false”语句。
回答by ceklock
This method does the trick.
这种方法可以解决问题。
boolean contains(char c, char[] array) {
for (char x : array) {
if (x == c) {
return true;
}
}
return false;
}
Example of usage:
用法示例:
class Main {
static boolean contains(char c, char[] array) {
for (char x : array) {
if (x == c) {
return true;
}
}
return false;
}
public static void main(String[] a) {
char[] charArray = new char[] {'h','e','l','l','o'};
if (!contains('q', charArray)) {
// Do something...
System.out.println("Hello world!");
}
}
}
Alternative way:
替代方式:
if (!String.valueOf(charArray).contains("q")) {
// do something...
}
回答by beat
Some other options if you do not want your own "Utils"-class:
如果您不想要自己的“Utils”类,还有其他一些选择:
Use Apache commons lang (ArrayUtils):
使用 Apache 公共语言(ArrayUtils):
@Test
public void arrayCommonLang(){
char[] test = {'h', 'e', 'l', 'l', 'o'};
Assert.assertTrue(ArrayUtils.contains(test, 'o'));
Assert.assertFalse(ArrayUtils.contains(test, 'p'));
}
Or use the builtin Arrays:
或者使用内置数组:
@Test
public void arrayTest(){
char[] test = {'h', 'e', 'l', 'l', 'o'};
Assert.assertTrue(Arrays.binarySearch(test, 'o') >= 0);
Assert.assertTrue(Arrays.binarySearch(test, 'p') < 0);
}
Or use the Charsclass from Google Guava:
或者使用Google Guava 中的Chars类:
@Test
public void testGuava(){
char[] test = {'h', 'e', 'l', 'l', 'o'};
Assert.assertTrue(Chars.contains(test, 'o'));
Assert.assertFalse(Chars.contains(test, 'p'));
}
Slightly off-topic, the Chars class allows to find a subarray in an array.
稍微偏离主题,Chars 类允许在数组中查找子数组。
回答by arenaq
From NumberKeyListenersource code. This method they use to check if char is contained in defined array of accepted characters:
来自NumberKeyListener源代码。他们使用这种方法来检查 char 是否包含在已定义的接受字符数组中:
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.
它类似于@óscarLópez 解决方案。由于缺少 foreach 迭代器,可能会更快一些。
回答by Erkan Ceylan
You can also define these chars as list of string. Then you can check if the characters is valid for accepted characters with list.Contains(x) method.
您还可以将这些字符定义为字符串列表。然后您可以使用 list.Contains(x) 方法检查字符是否对接受的字符有效。