I want to create a 2D array that each cell is an ArrayList!
If you want to create a 2D array of ArrayList.Then you can do this :
ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList(); // add another ArrayList object to [0,0]
table[0][0].add(); // add object to that ArrayList
Answer from AllTooSir on Stack OverflowI want to create a 2D array that each cell is an ArrayList!
If you want to create a 2D array of ArrayList.Then you can do this :
ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList(); // add another ArrayList object to [0,0]
table[0][0].add(); // add object to that ArrayList
The best way is to use a List within a List:
List<List<String>> listOfLists = new ArrayList<List<String>>();
Videos
I want to implement breadth first search in java. However I am stuck at making a adjacency list of the graph. The method on GeeksforGeeks declares the list as LinkedList<Integer> adj[]; Well this is actually an array of List and not dynamic too. Like you'll have to change the array size when you want to add some extra node. So, I tried List<List<Integer>> adj = new ArrayList<List<Integer>>(); and then made the inner ArrayList in a for loop as adj.add(new ArrayList<Integer>);. I am trying from so long still getting this error 'List cannot be resolved to a type'. I searched on Stack overflow and there too they have the same declaration as mine so I don't really know what I am doing wrong. Please help me.
Edit: I am using Java 11