How to create a List containing n copies of a specific character in Java

1 Answer

0 votes
package javaapplication1;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class JavaApplication1 {

    public static void main(String[] args) {

        List list = Collections.nCopies(10, "Z");

        Iterator itr = list.iterator();

        while (itr.hasNext()) 
            System.out.println(itr.next());
  }
}
  
/*
run:

Z
Z
Z
Z
Z
Z
Z
Z
Z
Z

*/

 



answered Sep 24, 2016 by avibootz
...