0

This is my code

public Solution(int n, int p) {
        this.n = n;
        this.p = p;
        graph = new ArrayList<>(n);
        for (int i = 0 ;i < n;i++) {
            graph.set(i, new ArrayList<>());
        }
        System.out.println(solve());
    }


public ArrayList<ArrayList<Integer>> graph;

I get this exception:

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

The value of n and p are 5, 5

5
  • 3
    Use graph.add to add elements to a list, not set. You can't set indexes past the list's current size. Commented Jul 3, 2017 at 20:24
  • we don't know what "set" and"solve" are doing, and the full stackTrace would help Commented Jul 3, 2017 at 20:24
  • Why don't you simply do graph.add(new ArrayList<>());? Commented Jul 3, 2017 at 20:24
  • Use add instead of set Commented Jul 3, 2017 at 20:24
  • also graph is an arraylist and not a map. so use graph.add(new Arraylist<>()) as each index can hold only a single object. Commented Jul 3, 2017 at 20:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.