Java中list.get(index)报错

1、list.get(index)中的index为负值异常

 严重:Exception occurred during processing request:-1

            java.lang.ArrayIndexOutOfBoundsException:-1


原因:if(null != list.get(list.size()-1) && null != list.get(list.size()-1).getValue())

           {

           }


分析:当list.size() = 0时,list.size()-1 = -1,而list.get(index)中的index大于等于0,故会抛出数组越界异常。


2、list.get(0)赋值异常

严重:Exception occurred during processing request:Index:0,Size:0

           java.lang.IndexOutOfBoundsException:Index:0,Size:0


原因:if(list.size() == 0)

          {

                 maxLabel = list.get(0).getLabel();

                 minLabel = list.get(0).getLabel();

          }


分析:当list.size = 0时,说明list为空,list.get(0).getLabel()就会抛出异常。

原文地址:https://www.cnblogs.com/hzcya1995/p/13315376.html