If you need an immutable list with multiple copies of the same
element, the nCopies(int n, Object element)
method of
the Collections
class returns just such the List
:
List fullOfNullList = Collection.nCopies(10, null);
By itself, that doesn't seem too useful. However, you can then
make the list modifiable by passing it along to another list:
List anotherList = new ArrayList(fullOfNullList);
This now creates a 10-element ArrayList
, where each
element is null
. You can now modify each element at
will, as it becomes appropriate.