In the Hidden Features of Java question, I was interested in the answer about instance initializers.
I was wondering how to modify this line :
List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};
in order to make it perform the same job with nested Arraylists:
ArrayList<ArrayList<Integer>> numbers = ...
is that possible?
I don’t really recommend this approach, as it creates an (anonymous) class for no good reason.
Use either:
or
For 2 levels, you can use:
With a static import you could even reduce it to this, if you really want to: