collectiontest.java

来自「Java程序设计大学教程程序源代码」· Java 代码 · 共 70 行

JAVA
70
字号
/** * <p>Title: Collection演示程序</p> * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: 刘艺</p> * * @author 刘艺 * @version 1.0 */package jbookch5.container;import java.util.*;//CollectionTest类public class CollectionTest {    public CollectionTest() {        Student ZhangSan=new Student("张三",90);        c1.add(ZhangSan);        c1.add("张三");        c1.add("李四");        c1.add(new Student("王武",85));        c1.add(new Student("赵榴",76));        c1.add(ZhangSan);        printCollection(c1);        c1.remove(ZhangSan);        c1.remove("张三");        printCollection(c1);        c2.add(ZhangSan);        c2.add("李四");        printCollection(c2);        c2.addAll(c1);        printCollection(c2);    }        public static void main(String[] args){        new CollectionTest();    }        private void printCollection(Collection c) {        System.out.println("---------------------");        Iterator it=c.iterator();        while (it.hasNext()){            System.out.println(it.next());        }            }        //Collection支持多种容器类的实现    Collection c1=new ArrayList();    Collection c2=new HashSet();    }//Student类class Student {    public Student(String n,int s) {        name=n;        score=s;    }        public String toString(){        String s=name+"  成绩:"+score;        return s;    }        String name;    int score;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?