gpt4 book ai didi

java - IndexOutOfBoundsException : Index: 0, 大小:0 2D Arraylist

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:26 24 4
gpt4 key购买 nike

I have this code

ArrayList<ArrayList<Integer>> Pareto_set_List=new ArrayList<ArrayList<Integer>>();
for(Optimization_Problem.General_Calculation.Get_Another_Solution=0;Optimization_Problem.General_Calculation.Get_Another_Solution<Input.General_Inputs.Monte_Carlo_Step;Optimization_Problem.General_Calculation.Get_Another_Solution++){
int Count_Location=0;
for(Optimization_Problem.General_Calculation.Count_year=0;Optimization_Problem.General_Calculation.Count_year<Input.General_Inputs.Analysis_Period;Optimization_Problem.General_Calculation.Count_year++){
//Run code Calculation not shown here.
for(int j=0;j<Input.General_Inputs.Num_Of_Ppes;j++){
Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add(EncodingUtils.getInt(solution.getVariable(j)));
Count_Location++;}
double[] objectives = solution.getObjectives();
//System.out.println("Solution " + (1) + ":");
Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add((int) Math.floor(objectives[0])); Count_Location++;
Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add((int) Math.floor(objectives[1])); Count_Location++;
Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add((int) Math.floor(objectives[2])); Count_Location++;
}
}
Optimization_Problem.General_Calculation.Count_year=0;
}
}

当我运行这个时,我收到此错误

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Implementation_Method.main(Implementation_Method.java:53)

我认为数组列表是动态的,但我不知道为什么收到此错误。我花了很多时间在这个问题上但没有解决它,非常感谢任何帮助。提前致谢。

最佳答案

Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add(EncodingUtils.getInt(solution.getVariable(j)));

在此之前,您似乎没有向 Pareto_set_List 添加任何 ArrayList 实例,因此 ArrayList 为空。当您尝试检索位于 Optimization_Problem.General_Calculation.Get_Another_Solution 的元素(似乎为 0)时,会引发异常。

您可以做的是将 ArrayList 添加到 Pareto_set_List,如下所示:

Pareto_set_List.add(new ArrayList<Integer>());

您可以找到有用的信息here和几个链接here .

关于java - IndexOutOfBoundsException : Index: 0, 大小:0 2D Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28512043/

24 4 0