【发布时间】:2015-11-28 06:11:00
【问题描述】:
我正在尝试将数组列表添加到 for 循环内的列表中。在我的代码中,场景 是数组列表,假设在每个 i 中都有一个新值。对于每个 i 我想将 scenario 保存在列表 list_of_scenarios 但我有这个错误。
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
关于如何修复它的任何想法?
int[] ActionArrayTest = new int[25];
int[] test27Augest = new int[75];
ArrayList<Hour> scenario = new ArrayList<Hour>();
List<ArrayList<Hour>> list_of_scenarios = new ArrayList<ArrayList<Hour>>();
for (int i = 1; i <= 100; i++) {
ActionArrayTest = FunctionA ();
test27Augest = Fill_the_prefrences(ActionArrayTest);
scenario = FunctionB(test27Augest);
list_of_scenarios.add(i, scenario);
}
【问题讨论】:
-
至少阅读引发异常的方法的文档。
-
您在位置 1 开始循环 .. 并且您的数组列表当时为空 .. 从位置 0 开始
-
只需使用
list_of_scenarios.add(scenario);或以0 而不是1 开头的i。
标签: java arrays list for-loop arraylist