IndexOutOfBoundsException:索引:0,大小:0 2D Arraylist
IndexOutOfBoundsException: Index: 0, Size: 0 2D Arraylist
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 and several links here。
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 and several links here。