zoukankan      html  css  js  c++  java
  • 容器填充

    class StringAddress {
        private String s;
    
        public StringAddress(String s) {
            this.s = s;
        }
    
        public String toString() {
            return super.toString() + " " + s;
        }
    }
    
    public class FillingLists {
        public static void main(String[] args) {
            List<StringAddress> list = new ArrayList<StringAddress>(
                    Collections.nCopies(3, new StringAddress("Hello")
                    ));//使用Collections包装类的nCopies()填充容器
            System.out.println(list);
            System.out.println();
            Collections.fill(list, new StringAddress("Word!"));
            //fill只能替换已经存在的元素,而不能添加新的元素
            System.out.println(list);
    
        }
    }

    运行结果:

    [com.ma.array.StringAddress@61bbe9ba Hello, com.ma.array.StringAddress@61bbe9ba Hello, com.ma.array.StringAddress@61bbe9ba Hello]

    [com.ma.array.StringAddress@610455d6 Word!, com.ma.array.StringAddress@610455d6 Word!, com.ma.array.StringAddress@610455d6 Word!]

    阁下何不同风起,扶摇直上九万里。
  • 相关阅读:
    缺少一个=出现的问题
    快速排序+归并排序
    ACwing简单题(14)
    浅谈#ifndef
    fstream 使用详解
    _stat函数的使用
    关于文件结构体的使用
    new的使用
    ACwing13题目
    ACwing13题
  • 原文地址:https://www.cnblogs.com/mlyun/p/10839054.html
Copyright © 2011-2022 走看看