java fill list with values code example

Example 1: 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]

Example 2: how to fill a list with a single value java

//x is the list and value is what it is to be set to
for (int i = 0; i < x.length; i++) {
  x[i] = value;
}

Tags:

Java Example