gpt4 book ai didi

java - 索引越界了,但是不行?

转载 作者:行者123 更新时间:2023-12-01 07:23:00 24 4
gpt4 key购买 nike

我不知道这是怎么发生的,也许我错过了一些简单的事情。

我正在尝试删除第一个索引(如果它存在),代码如下:

ArrayList<String> historyValues = new ArrayList<String>();
ArrayList<String> historyLabels = new ArrayList<String>();
some_big_process_which_populates_array()
if(historyLabels.size() >= 1 && historyValues.size() >= 1 ){
historyLabels.add("Not enough data yet");
historyValues.add("0.0");
}else{
Log.v("history_values", historyValues.toString());
historyValues.remove(0);
historyLabels.remove(0);
}

还有错误。空数组如何越过 size()检查?

    06-18 15:36:53.208  1510815108/com.rainforestautomation.android.energyvue V/history_values﹕ []
06-18 15:36:53.208 15108-15108/com.rainforestautomation.android.energyvue D/AndroidRuntime﹕ Shutting down VM
06-18 15:36:53.209 15108-15108/com.rainforestautomation.android.energyvue E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rainforestautomation.android.energyvue, PID: 15108
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)

编辑:

看来我的逻辑颠倒了。

最佳答案

你的比较是颠倒的。

if(historyLabels.size() >= 1 && historyValues.size() >= 1 ){

应该是

if(historyLabels.size() < 1 && historyValues.size() < 1 ){

关于java - 索引越界了,但是不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com