IndexOutOfBoundsException:Index:0,Size:0 2D Arraylist

时间:2015-02-14 03:17:10

标签: java eclipse arraylist 2d dimension

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)

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

1 个答案:

答案 0 :(得分:0)

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

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

您可以执行的操作是将ArrayList添加到Pareto_set_List,如下所示:

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

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