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

sarah
  • 1,111
  • 1
  • 8
  • 26
  • 3
    Use [`graph.add`](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(E)) to add elements to a list, not [`set`](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#set(int,%20E)). You can't `set` indexes past the list's current size. – khelwood Jul 03 '17 at 20:24
  • we don't know what "set" and"solve" are doing, and the full stackTrace would help – azro Jul 03 '17 at 20:24
  • Why don't you simply do `graph.add(new ArrayList<>());`? – QBrute Jul 03 '17 at 20:24
  • Use add instead of set – Jens Jul 03 '17 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. – digidude Jul 03 '17 at 20:26

0 Answers0