java基础学习(12)容器-2

xiaoxiao2021-02-28  94

填充容器

public static void main(String[] args) { //复制相同的对象传递给List构造器 List<String> strList=new ArrayList<String>(Collections.nCopies(4,"Hello")); System.out.println(strList); //fill也是复制同一个对象引用填充整个容器 //只对List对象有用,但是只能替换list中存在的元素,不能添加新元素 Collections.fill(strList,"World"); System.out.println(strList); } //结果 [Hello, Hello, Hello, Hello] [World, World, World, World]
转载请注明原文地址: https://www.6miu.com/read-46627.html

最新回复(0)