1

i want to passing value in arraylist, and initialized well. but there is an error -

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 0

I am trying hard, here is my code -


      public static void main(String[]args)
        {
            int n = 3;
            ArrayList<ArrayList<Integer>> graph = new ArrayList<>(n+2);
            for(int i=0; i < n+2 ; i++) {
                graph.add(new ArrayList<>(n+2));
            }
            graph.get(1).add(3,9);
            graph.get(2).add(3,1);
            graph.get(1).add(2,5);

            int s = 1, d = 3;
            System.out.println(dijkstra(s,d,graph));
        }

2 Answers 2

2

You initialize your ArrayLists with capacity = n + 2, but the ArrayLists are still empty when they are first created. The capacity is different than the size.

capacity
The number of elements that could be added to the ArrayList without allocating any additional memory
size
The number of elements actually stored in the ArrayList


Your calls to ArrayList::add are throwing an out of bounds exception because an empty ArrayList cannot insert a value at index = 3. By definition, the first value that is added to any ArrayList will have index = 0.

Why are you trying to insert values at particular indices? The solution will depend on why you are doing that.

EDIT: SOLUTION

When you create the ArrayList, initialize it with n + 2 entries all with value = 0.

...
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(n+2);
for(int i=0; i < n+2 ; i++) {
    ArrayList<Integer> sublist = new ArrayList<>(n+2);
    for (int j = 0; j < n + 2; j++) {
        sublist.add(0);
    }
    graph.add(sublist);
}
...

Keep the rest the same and it will behave as desired.

Sign up to request clarification or add additional context in comments.

2 Comments

i want to pass graph's vertices, edges and value
@DipChakraborty see my added solution
1

Problem

The following line means adding 9 at index, 3

graph.get(1).add(3,9);

However, graph.get(1) returns an ArrayList<Integer> which is empty. That's why you are getting the following error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 0

Solution

Replace

graph.get(1).add(3,9);

with

graph.get(1).add(9);

which will add the element (e.g. 9 in this case) after the last index where an element has been added. In this case, 9 will be added at index, 0.

Update

Alternatively, you can initialize the inner list as follows:

for(int i=0; i < n+2 ; i++) {
    List<Integer> list = new ArrayList<>();
    for(int j = 0; j< n+2; j++){
        list.add(0);
    }
    graph.add(list);
}

and then you can carry on as follows (the way you have already done):

graph.get(1).add(3,9);
graph.get(2).add(3,1);
graph.get(1).add(2,5);

Update 2

Given below is the concise way based on Andy Turner's comment:

for (int i = 0; i < n + 2; i++) {
    graph.add(new ArrayList<>(Collections.nCopies(n + 2, 0)));
}

6 Comments

i have to take two index
@DipChakraborty - in that case, you need to initialize the inner list with some values.
can you help how to do?
@DipChakraborty - I've updated my answer to fulfil this requirement. Feel free to comment in case of any doubt/issue.
new ArrayList<>(Collections.nCopies(n+2, 0)) is a more concise way to "Alternatively, you can initialize".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.