Collections.fill(List, E) in Java

Collections.fill(List, E): This method is available in java.util.Collections class of Java.

Syntax:

<E> void java.util.Collections.fill(List<? super E> list, E obj)

This method takes two arguments. This method replaces all of the elements of the specified list with the specified element.

Note: This method runs in linear time.

Parameters: Two parameters are required for this method.

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.

Approach

Java

import java.util.ArrayList;
import java.util.Collections;

public class Collectionsfill {
    public static void main(String[] args) {

        ArrayList<Integer> arrlist = new ArrayList<Integer>();

        arrlist.add(5);
        arrlist.add(178);
        arrlist.add(14);
        Collections.fill(arrlist, 10);

        System.out.println(arrlist);

    }
}

Output:

[10, 10, 10]


No comments:

Post a Comment