java.lang.IndexOutOfBoundsException [英] java.lang.IndexOutOfBoundsException

查看:89
本文介绍了java.lang.IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL查询返回的整数数组。

 的ArrayList<整数GT; intArray =新的ArrayList<>(44);而(result.next()){
   intArray.add(result.getInt(CNT)); //将结果转换成Java数组列表
}//将结果转换为Java对象
DC =新DCDataObj(
       intArray.get(1),//数据中心1000
       intArray.get(2),//区1100
       ..................
     )

当我运行code我得到这个错误:
java.lang.IndexOutOfBoundsException:指数:40,尺寸:40

你能告诉我哪里是我的错误,当我使用ArrayList <?/ p>

解决方案

而不是

  DC =新DCDataObj(
           intArray.get(1),//数据中心1000
           intArray.get(2),//区1100
           ...

使用

  DC =新DCDataObj(
           intArray.get(0),//数据中心1000
           intArray.get(1),//区1100
           ...

由于列表索引是从零开始的(就像数组和字符串)。


如果我是你,我想也许提供 DCDataObj 的一个构造函数一个列表&LT;整数GT; 作为参数,然后你可以简单地调用

  DC =新DCDataObj(intArray);

I have a SQL query which returns array of integers.

ArrayList<Integer> intArray = new ArrayList<>(44);

while (result.next()){
   intArray.add(result.getInt("CNT"));     // Insert the result into Java Array List
}

// Insert the result into Java Object
dc = new DCDataObj(
       intArray.get(1), //    Datacenter          1000
       intArray.get(2), //    Zone                1100
       ..................
     )

I get this error when I run the code: java.lang.IndexOutOfBoundsException: Index: 40, Size: 40

Can you tell me where is my mistake when I use ArrayList?

解决方案

instead of

dc = new DCDataObj(
           intArray.get(1), //    Datacenter          1000
           intArray.get(2), //    Zone                1100
           ...

use

dc = new DCDataObj(
           intArray.get(0), //    Datacenter          1000
           intArray.get(1), //    Zone                1100
           ...

As List indexes are zero based (just like arrays and strings).


If I were you, I'd maybe provide a constructor of DCDataObj that takes a List<Integer> as parameter and then you can simply call

dc = new DCDataObj(intArray);

这篇关于java.lang.IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆