Fill a list

ReturnMethodSummary
static<T> void fill(List<? super T> list, T obj) Replaces all of the elements of the specified list with the specified element.

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

public class Main {
  public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < 10; i++){
      list.add("j a v a2s.com");
    }
    System.out.println(list);
    
    Collections.fill(list, "java2s.com");
    System.out.println(list);
  }
}
  

The output:


[j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com, j a v a2s.com]
[java2s.com, java2s.com, java2s.com, java2s.com, java2s.com, java2s.com, java2s.com, java2s.com, java2s.com, java2s.com]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.