Java Collections fill()方法与示例

集合类fill()方法 (Collections Class fill() method)

  • fill() method is available in java.util package.

    fill()方法在java.util包中可用。

  • fill() method is used to fill all the elements in the given list(l) with the given element (ele).

    fill()方法用于用给定元素( ele )填充给定list( l )中的所有元素。

  • fill() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then also we will not get an error.

    fill()方法是静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则也不会收到错误。

  • fill() method may throw an exception at the filling the list with the given element.

    fill()方法可能会在使用给定元素填充列表时引发异常。

    UnsupportedOperationException: This exception may throw when the given parameter List(l) un-support set operation.

    UnsupportedOperationException :当给定参数List(l)不支持set操作时,可能引发此异常。

Syntax:

句法:

    public static void fill(List l, Type ele);

Parameter(s):

参数:

  • List l – represents the list object to be filled with given element(ele).

    列表l –表示要用给定element(ele)填充的列表对象。

  • Type ele – represents the element(ele) with which to replace the elements in the list.

    ele类型 –表示用来替换列表中元素的element(ele)。

Return value:

返回值:

The return type of this method is void, it returns nothing.

此方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program is to demonstrate the example
// of void fill() of Collections

import java.util.*;

public class Fill {
    public static void main(String args[]) {
        // Instantiate a LinkedList   
        List link_l = new LinkedList();

        // By using add() method is to
        // add elements in linked list
        link_l.add(10);
        link_l.add(20);
        link_l.add(30);
        link_l.add(40);
        link_l.add(50);

        // Display LinkedList
        System.out.println("link_l: " + link_l);

        // By using fill() method is to
        // fill the linked list with the
        // given element "100"
        Collections.fill(link_l, 100);

        //Display LinkedList
        System.out.println("Collections.fill(link_l,100): " + link_l);
    }
}

Output

输出量

link_l: [10, 20, 30, 40, 50]
Collections.fill(link_l,100): [100, 100, 100, 100, 100]


翻译自: https://www.includehelp.com/java/collections-fill-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值