【问题标题】:Issue with detecting an empty index in a Java char array在 Java char 数组中检测空索引的问题
【发布时间】:2018-05-23 17:58:20
【问题描述】:

我目前正在从事一个项目,在该项目中遇到了检测 char 数组中的空索引的问题。问题是它没有检测到数组中是否有空索引。我尝试检测人们所说的不同内容,其中空字符的占位符但它们似乎都不起作用。

以下是我尝试过的不同方法: 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

我还没有删除我所有的调试代码,所以请忽略它。 谢谢大家的帮助。

【问题讨论】:

  • “问题”是什么意思?你有错误吗?不是想要的输出? (如果是,请edit您的问题并包括错误/所需和当前输出)
  • arrayOfCorrect 是否应该将匹配的字母保持在它们在原始数组中的位置? (即[blank, 'P', blank, 'R']
  • arrayOfCorrect 应该只保存已经使用的字母。 (即['P','R',空白,空白])。我知道我使用的索引变量只有在创建数组后才会增加,但我认为这不是导致问题的原因。
  • counter 变量计数是什么?
  • 计数器变量正在计数,以查看当前正在查看的字符是否包含在 arrayOfCorrect 中,抱歉我的变量名称不好,最后我把东西放在一起了。

标签: java arrays iteration


【解决方案1】:

嗯,好的。首先,问题不在于检测到空字符,我自己用这段代码测试过:

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” 将始终为真:您在 for 循环遇到空字符串之前就退出了它。你有“arrayOfCorrect = ['P', 'R', empty, empty]。

这是您的代码的一部分:

if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
    System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
    counter++;
    break;
} 

当您使用 i 遍历 array1 时,当 i 等于 0 和 2 时条件为真。从您的结果可以看出,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') 时为真,并且您在那里退出嵌套循环。

当 i = 2 时,"getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)" 当 f = 0 ('S' != 'P') 时为真,并且您在那里退出嵌套循环。

当 i = 3 时,"getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)" 当 f = 0 ('R' != 'P') 时为真,并且您在那里退出嵌套循环。

是的,counter++ 只被调用了 3 次,而且你永远不会遇到空字符。

希望这会有所帮助。

【讨论】:

  • 这真的很有帮助。现在我知道发生了什么,我可以解决这个问题。
猜你喜欢
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 2011-06-11
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 2018-11-14
相关资源
最近更新 更多