【问题标题】:IndexOutOfBoundsException: Index: 0, Size: 0 2D ArraylistIndexOutOfBoundsException:索引:0,大小:0 2D Arraylist
【发布时间】:2015-04-15 05:30:27
【问题描述】:
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)

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

【问题讨论】:

    标签: java eclipse arraylist 2d dimension


    【解决方案1】:
    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

    【讨论】:

    • 感谢回复,但我已经在开头定义了数组列表 ArrayList> Pareto_set_List=new ArrayList>();
    • 我是否需要在此之前执行此步骤,您的意思是 Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add(EncodingUtils.getInt(solution.getVariable(j)));
    • 您可能已经定义并初始化了Pareto_set_List,但是在调用get 之前,您是否向其中添加任何ArrayList 实例?如果不这样做,则 ArrayList 的大小将为 0,并且尝试访问任何索引处的元素将导致抛出异常。
    • 不,我不知道,因为这是一个二维数组列表,所以我需要调用 get 和 add together 我不知道任何其他方式你可以举一个例子来说明如何将任何 ArrayList 实例添加到请在调用之前获取它
    • 是的,但是在空的 ArrayList 上调用 get 会引发异常,这就是我要解决的问题。您所要做的就是在调用get 之前将ArrayList 添加到Pareto_set_List
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多