Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
IBM : developerWorks : Java : Education - online courses
Java Collections Framework
Download tutorial zip fileView letter-sized PDF fileView A4-sized PDF fileE-mail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPreviousNext
4. Special collection implementations
  


Multiple copy collections page 5 of 6


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.


Main menuSection menuGive feedback on this tutorialPreviousNext
PrivacyLegalContact