java fill list

Solutions on MaxInterview for java fill list by the best coders in the world

showing results for - "java fill list"
Jordy
02 Apr 2019
1// -- Filling with fill() method example:
2List<String> arrlist = new ArrayList<String>();  
3//Add elements in the list  
4arrlist.add("one");  
5arrlist.add("two");  
6arrlist.add("three");  
7// contents of list: [AAA, BBB, CCC]
8
9//Fill the list with 'four'  
10Collections.fill(arrlist,"four");  
11// contents of list: [four, four, four]
12
13// -- Second example                
14List<Integer> arrList = Arrays.asList(1,2,3,4);      
15//Fill the list with 551  
16Collections.fill(arrList,42);
17// contents of list: [42, 42, 42]
Lennart
13 Jul 2019
1//x is the list and value is what it is to be set to
2for (int i = 0; i < x.length; i++) {
3  x[i] = value;
4}