當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Collections fill()用法及代碼示例


java.util.Collections類的fill()方法用於將指定列表的所有元素替換為指定元素。

此方法以線性時間運行。

用法:


public static  void fill(List list, T obj)

參數:該方法將以下參數作為參數

  • list –用指定元素填充的列表。
    obj –用來填充指定列表的元素。

    以下示例說明了fill()方法

    示例1:

    // Java program to demonstrate 
    // fill() method 
    // for String value 
      
    import java.util.*; 
      
    public class GFG1 { 
        public static void main(String[] argv) throws Exception 
        { 
            try { 
      
                // creating object of List<Integer> 
                List<String> arrlist = new ArrayList<String>(); 
      
                // Adding element to srclst 
                arrlist.add("A"); 
                arrlist.add("B"); 
                arrlist.add("C"); 
      
                // print the elements 
                System.out.println("List elements before fill: "
                                   + arrlist); 
      
                // fill the list 
                Collections.fill(arrlist, "TAJMAHAL"); 
      
                // print the elements 
                System.out.println("\nList elements after fill: "
                                   + arrlist); 
            } 
      
            catch (IllegalArgumentException e) { 
                System.out.println("Exception thrown : " + e); 
            } 
      
            catch (NoSuchElementException e) { 
                System.out.println("Exception thrown : " + e); 
            } 
        } 
    }
    輸出:
    List elements before fill: [A, B, C]
    
    List elements after fill: [TAJMAHAL, TAJMAHAL, TAJMAHAL]
    

    示例2:

    // Java program to demonstrate 
    // fill() method 
    // for Integer value 
      
    import java.util.*; 
      
    public class GFG1 { 
        public static void main(String[] argv) throws Exception 
        { 
            try { 
      
                // creating object of List<Integer> 
                List<Integer> arrlist = new ArrayList<Integer>(); 
      
                // Adding element to srclst 
                arrlist.add(20); 
                arrlist.add(30); 
                arrlist.add(40); 
      
                // print the elements 
                System.out.println("List elements before fill: "
                                   + arrlist); 
      
                // fill the list 
                Collections.fill(arrlist, 500); 
      
                // print the elements 
                System.out.println("\nList elements after fill: "
                                   + arrlist); 
            } 
      
            catch (IllegalArgumentException e) { 
                System.out.println("Exception thrown : " + e); 
            } 
      
            catch (NoSuchElementException e) { 
                System.out.println("Exception thrown : " + e); 
            } 
        } 
    }
    輸出:
    List elements before fill: [20, 30, 40]
    
    List elements after fill: [500, 500, 500]
    


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections fill() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。