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

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

问题描述

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

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
       ..................
     )

运行代码时出现此错误:java.lang.IndexOutOfBoundsException:索引:40,大小:40

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

你能告诉我在使用 ArrayList 时我的错误在哪里吗?

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

推荐答案

代替

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

使用

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

As List 索引是从零开始的(就像数组和字符串一样).

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

如果我是你,我可能会提供一个 DCDataObj 的构造函数,它接受一个 List 作为参数,然后你可以简单地调用

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天全站免登陆