集合类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