Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异常:比较两个不同字符串数组的两个字符串时发生java.lang.NullPointerException_Java_Nullpointerexception_Arrays_Compareto - Fatal编程技术网

异常:比较两个不同字符串数组的两个字符串时发生java.lang.NullPointerException

异常:比较两个不同字符串数组的两个字符串时发生java.lang.NullPointerException,java,nullpointerexception,arrays,compareto,Java,Nullpointerexception,Arrays,Compareto,我正在通过codingbat.com/java编写代码,遇到了一个我不理解的错误。我得到了两个字符串数组并想比较它们。如果我只是使用数组,所有的工作都很好(但结果不正确)。为了得到正确的结果,我编程了一个helper函数,它消除了数组的所有重复项。我测试了helper函数,它返回重复的数组 我可以使用\u a[I]等检索新数组中的值,并且不会出现错误,但是如果我使用\u a[0]。等于(\u b[0])或(\u a[0])。比较(\u b[0])我会得到一个NullPointerExceptio

我正在通过codingbat.com/java编写代码,遇到了一个我不理解的错误。我得到了两个字符串数组并想比较它们。如果我只是使用数组,所有的工作都很好(但结果不正确)。为了得到正确的结果,我编程了一个helper函数,它消除了数组的所有重复项。我测试了helper函数,它返回重复的数组

我可以使用
\u a[I]
等检索新数组中的值,并且不会出现错误,但是如果我使用
\u a[0]。等于(\u b[0])或(\u a[0])。比较(\u b[0])
我会得到一个
NullPointerException(\u a[0]=\u b[0]工作正常…

如果我只使用原始数组a、b,代码运行不会出现问题。我不明白为什么我会在那里得到一个NullpointerException

谢谢你的帮助

代码:

public int commonTwo(字符串[]a,字符串[]b){
字符串[]_a=重复(a);
字符串[]_b=killreplicate(b);
int ai=0,bi=0,count=0;
对于(int i=0;ai<\u a.length&bi<\u b.length;i++){
如果(_a[ai].compareTo(_b[bi])>0{//NullPointerException,但如果我使用a,b
bi++;
}else如果(_a[ai].compareTo(_b[bi])<0{//NullPointerException,但如果我使用a,b
ai++;
}否则{
计数++;
ai++;
bi++;
}  
}
返回计数;
}
辅助功能:

 public String[] killDuplicate(String[] a){

     String temp = "";
     int counter = 0, counter2 = 0;

     for (int i = 0; i < a.length; i++){
        if (! a[i].equals(temp)){
           temp = a[i];
        } else {
           a[i] = "";
           counter++;
        }
     }

     String[] result = new String[a.length - counter];

     for (int i = 0; counter2 < counter; i++){
        if (a[i].equals("")) {
           counter2++;
        }
     } else {
        result[i-counter2] = a[i];
     }
     return result;
 }
公共字符串[]重复(字符串[]a){
字符串temp=“”;
int计数器=0,计数器2=0;
for(int i=0;i
我猜您假定字符串数组已排序,否则您的
killDuplicate
方法将毫无意义

代码的问题在于,在
killDuplicate
方法中的第二个
for
循环中,您使用条件
counter2
进行迭代,该条件表示迭代直到所有找到的重复项都被传递。因此,当您找到最后一个副本时,您将退出,而不填充阵列的其余部分。尝试使用示例:
新字符串[]{“A”、“A”、“B”、“C”}
您将得到
[A,null,null]

除了下面代码的最简单修改之外,还有很多地方可以改进。(我只为
循环更改了第二个
)
公共字符串[]重复(字符串[]a){

String temp=”“;
int计数器=0,计数器2=0;
for(int i=0;i
\u a[ai]
\u b[bi]
返回的项为空,因此您的错误。k,我的助手函数似乎是错误的,我只是有一个错误的测试用例,它返回了一个正确的值,但它没有返回其他值。我试着把它修好。编辑:是的,helper函数中有一个bug,请参见下面的答案,谢谢。是的,你是对的,我忘了在这个练习中提到数组是被排序的。其余的也是对的,谢谢:)
 public String[] killDuplicate(String[] a){

     String temp = "";
     int counter = 0, counter2 = 0;

     for (int i = 0; i < a.length; i++){
        if (! a[i].equals(temp)){
           temp = a[i];
        } else {
           a[i] = "";
           counter++;
        }
     }

     String[] result = new String[a.length - counter];

     for (int i = 0; counter2 < counter; i++){
        if (a[i].equals("")) {
           counter2++;
        }
     } else {
        result[i-counter2] = a[i];
     }
     return result;
 }
    String temp = "";
    int counter = 0, counter2 = 0;

    for (int i = 0; i < a.length; i++) {
        if (!a[i].equals(temp))
            temp = a[i];
        else {
            a[i] = "";
            counter++;
        }
    }

    String[] result = new String[a.length - counter];

    for (int i = 0; i < a.length; i++) {
        if (a[i].equals("")) continue;
        result[counter2] = a[i];
        counter2++;
    }

    return result;
}