how to make a class method fill objects in arraylist code example

Example: java fill list

// -- Filling with fill() method example:
List<String> arrlist = new ArrayList<String>();  
//Add elements in the list  
arrlist.add("one");  
arrlist.add("two");  
arrlist.add("three");  
// contents of list: [AAA, BBB, CCC]

//Fill the list with 'four'  
Collections.fill(arrlist,"four");  
// contents of list: [four, four, four]

// -- Second example                
List<Integer> arrList = Arrays.asList(1,2,3,4);      
//Fill the list with 551  
Collections.fill(arrList,42);
// contents of list: [42, 42, 42]

Tags:

Java Example