简体   繁体   中英

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;
        }
    }

When I run this I get this error

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)

I thought the array list is dynamic and I have no idea why I received this error. I spend a lot of time on this without solving it any help is highly appreciated. Thanks in advance.

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

Before this point, you don't seem to add any ArrayList instances to Pareto_set_List , so the ArrayList is empty. When you try to retrieve the element located at Optimization_Problem.General_Calculation.Get_Another_Solution (which seems to be 0), the exception is thrown.

What you could do is add an ArrayList to Pareto_set_List as follows:

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

You can find helpful information here and several links here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM