【问题标题】:Loop is infinitly looping or IndexOutOfBoundsException: Index =4, Size =2循环无限循环或 IndexOutOfBoundsException: Index =4, Size =2
【发布时间】:2017-06-27 22:25:04
【问题描述】:

在这个函数中,当我使用 For..Loop 时,它会无限循环,而当我使用 While..Loop 时,我得到了错误:IndexOutOfBoundsException。我想要做的是将一定范围内的 imageInfosArrayList 中的元素复制到 restrictedAreaArrayList 中,代码如下:

 @Override
public void onLocationChanged(Location location) {
    //Toast.makeText(this,"Location changed!! wait for coordiates update",Toast.LENGTH_LONG).show();
    mLocation=location;


    latitudeMe=mLocation.getLatitude();
    longitudeMe=mLocation.getLongitude();
    Log.i(TAG,String.valueOf(latitudeMe+","+longitudeMe));

    getMyLocations(latitudeMe,longitudeMe);
    //getLocations("München Flughafen");
    Log.i(TAG,"Ziel coordinates "+String.valueOf(latitudeZiel+","+latitudeZiel));
   // for (int i = 0; i < imageInfosArrayList.size(); i++) {

    int i=0;
    while(i<imageInfosArrayList.size()){
        Log.e(TAG,imageInfosArrayList.toString());
        LatLng hier = getLocations(imageInfosArrayList.get(i).getPlace());
        if (hier != null) {
            Log.i(TAG, imageInfosArrayList.get(i).getPlace());

            distanceGeo = distance(hier.latitude, hier.longitude);
            ImageInfos imageInfos= new ImageInfos(mContext);



            if (distanceGeo <= Float.valueOf(distance[0])) {
                imageInfos= imageInfosArrayList.get(i);

                restrictedAreaArrayList.add(i,imageInfos);
                Log.e(TAG, "restrictedarea filling..." + restrictedAreaArrayList.size());
                i++;
            }
        } else {
            Log.i(TAG, "Can't locate this adress..." + imageInfosArrayList.get(i).getPlace());
            i++;
        }
    }

}

这里是 Logcat:

com.historyimages E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: bayram.com.historyimages, PID: 9792
                                                                    java.lang.IndexOutOfBoundsException: Index: 4, Size: 2
                                                                        at java.util.ArrayList.add(ArrayList.java:457)
                                                                        at bayram.com.historyimages.Umgebung.UmgebungFragment.onLocationChanged(UmgebungFragment.java:394)
                                                                        at com.google.android.gms.internal.zzasg$zzb$1.zza(Unknown Source)
                                                                        at com.google.android.gms.internal.zzasg$zzb$1.zzs(Unknown Source)
                                                                        at com.google.android.gms.internal.zzabh.zzb(Unknown Source)
                                                                        at com.google.android.gms.internal.zzabh$zza.handleMessage(Unknown Source)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                        at android.os.Looper.loop(Looper.java:156)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6524)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

【问题讨论】:

  • 尝试将 i++ 移到 if/else 块之外
  • 添加错误日志或添加有关发生错误的行的详细信息

标签: java android arraylist


【解决方案1】:

您在restrictedAreaArrayList 上收到IndexOutOfBoundsException, 您正在添加 imageInfosrestrictedAreaArrayList

restrictedAreaArrayList.add(i,imageInfos);

restrictedAreaArrayList 中可能不存在第 i 个索引,您可以添加它

restrictedAreaArrayList.add(imageInfos)

或者,如果您想保留订单,则将 restrictedAreaArrayList 的大小设置为等于 imageInfosArrayList 你可以通过创建它来做到这一点

restrictedAreaArrayList = new ArrayList&lt;ImageInfos&gt;(imageInfosArrayList.size());

或者

你可以打电话

restrictedAreaArrayList.ensureCapacity(imageInfosArrayList.size())

确保它与imageInfosArrayList具有相同的容量

【讨论】:

    【解决方案2】:

    好吧,我们没有您的 for 循环示例的代码,但我怀疑它在 while 循环中包含类似的错误。

    您的循环,简化后如下所示:

    int i = 0;
    while (i < size_of_x) {
        y = getlocations(h.get(i));
        if (y != null) {
            do_something()
            i++;
        }
        else {
            i++;
        }
    }
    

    让我们假设 h 只有 2 个元素,所以 get(0) 和 get(1) 可以工作,但 get(2) 会失败。您的循环以以下行为运行:

    1. i = 0; getlocations(h.get(0)) 返回 NULL; i = 1。
    2. i = 1; getLocations(h.get(1)) 返回 NULL; i = 2。
    3. i = 2; getLocations(h.get(2)) 失败,因为索引超出范围,因此您会收到 IndexOutOfBoundsException。

    我猜您的 for 循环版本与您发布的代码所暗示的差异更多,但可能会“无限”运行,因为您的终止条件不满足,因为 getlocations() 调用不成功.

    您需要重新考虑您在循环中所做的事情,尤其是在您进行可能失败的函数调用时。您还应该练习我上面描述的练习,遍历您的代码,并检查循环中的行为是否符合您的预期或想要的。

    【讨论】:

    • 我已经做到了,但前 2 个元素不是 NUll,至少我确信前 2 个元素应该存在。
    • @Bayram 前两个元素是什么?您的代码中有多个列表以及生成异常的多种方法。你如何“确定”它?你真的检查了吗?
    • 是的,我尝试了其他方法,而不是填充新的 Arraylist,我使用了 remove(i),所以从 6 开始我得到了 4。但现在问题是当我尝试将 imageinfosarraylist 传递给myrecyclerview 的适配器我检测到不一致。以下是我的 infoarraylist 的元素:[bayram.com.historyimages.DataBase.ImageInfos@e4dcea,bayram.com.historyimages.DataBase.ImageInfos@3bd8bdb,bayram.com.historyimages.DataBase.ImageInfos@eb37678,bayram.com.historyimages .DataBase.ImageInfos@9523151]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多