How do we create arraylist dynamically inside a loop?
something like -
for(i=0;i<4;i++)
{
List<Integer> arr(i) = new ArrayList<>();
}
It sounds like what you actually want is a list of lists:
List<List<Integer>> lists = new ArrayList<List<Integer>>();
for (int i = 0; i < 4; i++) {
List<Integer> list = new ArrayList<>();
lists.add(list);
// Use the list further...
}
// Now you can use lists.get(0) etc to get at each list
EDIT: Array example removed, as of course arrays of generic types are broken in Java :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With