1
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.

1 Answers1

0
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.

Community
  • 1
  • 1
TNT
  • 2,900
  • 3
  • 23
  • 34
  • thanks for replying but i already defined the array list at the beginning ArrayList> Pareto_set_List=new ArrayList>(); – Ahmad Ghazi Altarabsheh Feb 14 '15 at 03:29
  • do I need to do this step just before this point you mean Pareto_set_List.get(Optimization_Problem.General_Calculation.Get_Another_Solution).add(EncodingUtils.getInt(solution.getVariable(j))); – Ahmad Ghazi Altarabsheh Feb 14 '15 at 03:30
  • You may have defined and initialized `Pareto_set_List`, but did you *add* any `ArrayList` instances to it before calling `get`? If you don't do so, the size of the ArrayList will be 0, and trying to access an element at any index will result in the exception being thrown. – TNT Feb 14 '15 at 03:32
  • No I don't because this is a two dimensional arraylist so I need to call get and add together I don't know any other way CAn you give an example of how to add any ArrayList instances to it before calling get please – Ahmad Ghazi Altarabsheh Feb 14 '15 at 03:36
  • Yes, but calling `get` on an empty ArrayList throws an exception, which is the point I'm trying to get across. All you have to do is add an `ArrayList` to `Pareto_set_List` before you call `get`. – TNT Feb 14 '15 at 03:41
  • I really don't know how to do it can you give me simple example please I still new to 2d arraylist thanks – Ahmad Ghazi Altarabsheh Feb 14 '15 at 03:44
  • See my edit. I think it's a lot easier to search for information on your own next time. – TNT Feb 14 '15 at 03:55