我目前正在开发一个项目,在该项目中,我遇到了检测字符数组中的空索引的问题。问题是它不检测数组中是否有空索引。我尝试过检测人们所说的不同内容,其中空字符的占位符但它们似乎都不起作用。
以下是我尝试过的不同方法: 0、“0”、“u0000”、“\u0000\”和 null。
public class TestArrayChecker {
public static void main(String[] args) {
char array1[] = new char[] {'F', 'P', 'S', 'R'};
char array2[] = new char[] {'S', 'P', 'O', 'R'};
char c1 = '\u0000';
char arrayOfCorrect[] = new char[array1.length];
int correct = 0;
int counter = 0;
int close = 0;
int index = 0;
for (int i = 0; i < 4; i++) {
if (getColorAt(i, array1) == getColorAt(i, array2)) {
correct++;
arrayOfCorrect[index] = getColorAt(i, array1);
index++;
}
}
for (int i = 0; i < 4; i++) {
for (int n = -i; n < 4 - i; n++) {
if(getColorAt(i, array1) == getColorAt(i + n, array2)) {
for (int f = 0; f < arrayOfCorrect.length; f++) {
System.out.println(getColorAt(f, arrayOfCorrect) + " " + f);
System.out.println(arrayOfCorrect.length);
System.out.println(getColorAt(f, arrayOfCorrect) != c1);
if (getColorAt(f, arrayOfCorrect) != c1) {
System.out.println(getColorAt(f, arrayOfCorrect) + " " + f);
System.out.println("No Void in this array");
if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
counter++;
break;
}
} else {
counter++;
System.out.println("We got here!");
}
if (counter == 4) {
close++;
}
}
}
}
}
System.out.println(arrayOfCorrect[0] + " " + arrayOfCorrect[1] + " " + arrayOfCorrect[2] + " " + arrayOfCorrect[3]);
System.out.println(correct);
System.out.println(close);
System.out.println(counter);
}
public static char getColorAt(int index, char array[]) {
// TODO: Fill-in code to return the color at a particular position
return array[index];
}
}
这是代码不起作用的主要部分:
if (getColorAt(f, arrayOfCorrect) != c1) {
System.out.println(getColorAt(f, arrayOfCorrect) + " " + f);
System.out.println("No Void in this array");
if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
counter++;
break;
}
} else {
counter++;
System.out.println("We got here!");
}
这是控制台的输出,最后两个数字应该打印出 1 和 4。
P 0
4
true
P 0
No Void in this array
R 1
4
true
R 1
No Void in this array
This number is close: R
P 0
4
true
P 0
No Void in this array
This number is close: P
P 0
4
true
P 0
No Void in this array
This number is close: P
P R
2
0
3
我还没有删除所有的调试代码,所以请忽略它。 感谢大家的帮助。
最佳答案
嗯嗯,好吧。首先,问题不在于检测空字符,我自己用以下代码进行了测试:
public static void main(String[] args) {
char array[] = new char[10];
int emptyCounter = 0;
for(int i = 0; i < array.length; i++) {
if (array[i] == 0) {
emptyCounter++;
}
}
System.out.println(emptyCounter);
}
我得到的输出为 10,因为我将所有字符留空。
现在,查看您的代码,我们可以明白为什么“getColorAt(f, arrayOfCorrect) != c1”将始终为 true :您在 for 循环遇到空字符串之前就中断了它。你有“arrayOfCorrect = ['P','R',空,空]。
这是您的代码的一部分:
if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
counter++;
break;
}
当您使用 i 迭代 array1 时,当 i 等于 0 和 2 时,条件为 true。正如我们从结果中看到的,f 永远不会高于 1,但这不是问题的根源。
当我们进一步寻找问题的根源时,我们遇到了以下问题:
for (int n = -i; n < 4 - i; n++) {
if(getColorAt(i, array1) == getColorAt(i + n, array2)) {
...
}
}
顺便说一句,这不是一个好的做法...这就像做 2 + 10 - 10 + 2 = 4。据我所知,这会做同样的工作:
for (int n = 0; n < 4; n++) {
if(getColorAt(i, array1) == getColorAt(n, array2)) {
...
}
}
现在更容易理解您在这里想要做什么。
因此,对于 array1 中的每个字符,您检查 array2 中的每个字符,直到找到对应项。所以基本上,条件只会成立 3 次:当 array1[1] = array2[1]、array1[2] = array2[0] 和 array1[3] = array2[3] 时。
当 i = 1 时,“getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)”在 f = 1 ('P' != 'R') 时为 true,并且您退出嵌套循环。
当 i = 2 时,“getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)”在 f = 0 ('S' != 'P') 时为 true,并且您退出嵌套循环。
当 i = 3 时,“getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)”在 f = 0 ('R' != 'P') 时为 true,并且您退出嵌套循环。
所以,是的,counter++ 仅被调用 3 次,并且您永远不会遇到空字符。
希望这有帮助。
关于java - 检测 Java 字符数组中的空索引时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50494674/