且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将值设置为JTable时,索引超出范围异常

更新时间:2023-12-03 11:30:22

原因是您试图在ArrayList中某个位置设置一个值,该值大于列表的大小.最初创建的ArrayList的大小为0.请阅读有关ArrayList的文档和教程.

The reason is that you are trying to set a value at a position in the ArrayList that is bigger then the size of the list. Initially ArrayList is created with size 0. Please read the documentation and tutorials about ArrayList.

代码中还有其他问题,例如当您尝试为布尔值赋值时:

There are also other problems in the code, e.g. when you try to assign a value to a boolean:

    al = new ArrayList<Food>();
    bools = new ArrayList<>(al.size());
    for(Boolean b: bools){
        b=Boolean.FALSE;
    }

这基本上什么都不做.首先,列表在创建时为空,其次,赋值仅给b赋了一个新值,而不像您期望的那样更改bool中任何元素的内容.

This basically does nothing. First of all, the list is empty when created, second, the assignment only assigns a new value to b, but doesn't change the contents of any elements in bools, as you might expect it to do.