比较数据编号,Iterator.remove()抛出NullPointerException

时间:2017-05-06 12:39:20

标签: java android arraylist iterator

我正在尝试制作一款可以比较整数的Android应用。但是,当我尝试比较整数并且我需要删除一些条目因为它们是多余的或者它们不适合时,我不能使用Iterator.remove()而不会遇到NullPointerException。这是具体的输出:

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.daita.musicale, PID: 22649
          java.lang.UnsupportedOperationException
              at java.util.AbstractList.remove(AbstractList.java:638)
              at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
              at com.daita.musicale.MainActivity.checkNotes(MainActivity.java:115)
              at com.daita.musicale.MainActivity$3.onClick(MainActivity.java:74)
              at android.view.View.performClick(View.java:5198)
              at android.view.View$PerformClick.run(View.java:21147)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:148)
              at android.app.ActivityThread.main(ActivityThread.java:5417)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是给我错误的代码区域(在com.daita.musicale.MainActivity.checkNotes(MainActivity.java:115))。

public boolean[] checkNumbers(String code1, String code2) {
    if (code != null) {
        List<String> c1 = Arrays.asList(code1.replace("-1.0,", "").replace("null", "").split(","));
        List<String> c2 = Arrays.asList(code2.replace("-1.0,", "").replace("null", "").split(","));
        Iterator<String> c1iterator = c1.iterator();
        Iterator<String> c2iterator = c2.iterator();
        boolean strike = false;
        double prev = 0;
        int i = 0;
        boolean[] correctness = new boolean[c1.size()];
        while (c1iterator.hasNext()) {
            double c1hertz = Double.parseDouble(c1iterator.next());
            double c2hertz = Double.parseDouble(c2iterator.next());
            double difference = c1hertz - c2hertz;
            if (difference > -1 && difference < 1) {
                correctness[i] = true;
                double prevDifference = prev - c1hertz;
                if (prevDifference > -1 && prevDifference < 1) {
                    c1iterator.remove(); //specific issue location! (this is line 115 of MainActivity)
                    c2iterator.remove();
                    i = 0;
                    correctness = new boolean[c1.size()];
                } else {
                    prev = c1hertz;
                    correctness[i] = true;
                }
            } else {
                if (strike) {
                    correctness[i] = false;
                    try {
                        correctness[i - 1] = false;
                    } catch (ArrayIndexOutOfBoundsException aioobe) {
                        aioobe.printStackTrace();
                    }
                } else {
                    correctness[i] = true;
                }
            }
            i++;
        }
        return correctness;
    } else {
        return null;
    }
}

我已经查看了Code Review和Stack Overflow的其他问题,例如:

Calling remove in foreach loop in Java Removing object from ArrayList in for each loop 在迭代它时删除List上的元素 我非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

使用ArrayList,因为它支持使用迭代删除

List<String> c1 = new ArrayList<>(Arrays.asList(code1.replace("-1.0,", "").replace("null", "").split(",")));
List<String> c2 = new ArrayList<>(Arrays.asList(code2.replace("-1.0,", "").replace("null", "").split(",")));