Collections static void fill(List list,T obj) method Example Program


Replaces all of the elements of the specified list with the specified element.
This method runs in linear time.

Program

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
/**
 * 
 * @author karthikeyan.T
 * @description The following example shows the usage of Collections.fill()
 */
public class Fill {
		   public static void main(String args[]) {
		   // create array list object       
		   ArrayList arrlist = new ArrayList();
		      
		   // populate the list
		   arrlist.add("Karthik");
		   arrlist.add("Kamal");
		   arrlist.add("Mohan");
		   arrlist.add("vinodh");
		   arrlist.add("anand");
		   System.out.println("List elements before fill: "+arrlist);
		   Collections.fill(arrlist,"Candid");
		      
		   System.out.println("List elements after fill: "+arrlist);    
		   }    
		}

Output

List elements before fill: [Karthik, Kamal, Mohan, vinodh, anand]
List elements after fill: [Candid, Candid, Candid, Candid, Candid]

Explanation

public static <T> void fill(List<? super T> list,T obj)
Replaces all of the elements of the specified list with the specified element.
This method runs in linear time.

Parameters:
list - the list to be filled with the specified element.
obj - The element with which to fill the specified list.
Throws:
UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.


Related Post

Comments


©candidjava.com