initializing a multidimensional list with n empty 1D lists in Java -
i have multi-dimensional list
list<list<integer>> mylist; i want dimension specified @ run time, in code, put:
mylist = collections.synchronizedlist(new arraylist<list<integer>>(n)); i hoping initialize mylist list n elements each having 0 elements didn't happen. empty list. apparently constructor, "constructs empty list specified initial capacity." not quite want.
i understand can loop on mylist , add() empty one-dimensional lists, there way of achieving want less codes of code?
first of constructor arraylist<t>(int capacity) not insert element in list, not specifying size initial capacity.
basically allow list insert n elements without need of internal resizing.
so outer list still empty. can't use collections.fill because need different internal list<integer> every time, , fill set elements same reference. forced insert them manually:
for (int = 0; < n; ++i) mylist.add(new arraylist<integer>()); mind in case, since specified list<list<integer>> (which makes sense), java wouldn't able default initialize anything, since list<integer> in interface.
Comments
Post a Comment