zoukankan      html  css  js  c++  java
  • java_18 Collection接口

    1.Collection接口

      Collection 层次结构 中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。

       List接口特点:

            1.元素存储有序的集合。

            2.带索引的集合。

            3.集合可以有重复的元素。

            4.常用子类:ArrayList、LinkedList

    2.方法

      (1)clear()  移除此 collection 中的所有元素。

        public static void main(String[] args) {
    		Collection<String> co = new ArrayList<String>();
    		co.add("a");        //添加数据
    		co.add("b");
    		co.add("c");
    		System.out.println(co);
    		co.clear();        //清除数据
    		System.out.println(co);
    	}
    

     

      (2)contains()   如果此 collection 包含指定的元素,则返回 true

        public static void main(String[] args) {
    		Collection<String> co = new ArrayList<String>();
    		co.add("a");
    		co.add("b");
    		co.add("c");
    		boolean b = co.contains("a");
    		System.out.println(b);
    	}
    

     

      (3)toArray()   返回包含此 collection 中所有元素的数组

        public static void main(String[] args) {
    		Collection<Integer> co = new ArrayList<Integer>();
    		co.add(1);
    		co.add(2);
    		co.add(3);
    		Object[] array = co.toArray();
    		for (int i = 0; i < array.length; i++) {
    			System.out.println(array[i]);
    		}
    	}
    

     

    3Collection遍历

        public static void main(String[] args) {
    		Collection<Integer> co = new ArrayList<Integer>();
    		co.add(1);
    		co.add(2);						//添加数据
    		co.add(3);
    		Iterator it = co.iterator();	//生成co变量的迭代器来遍历集合
    		while(it.hasNext()) {			//hasnext():如果仍有元素可以迭代,则返回 true。
    			Object s = it.next();		//next():返回迭代的下一个元素
    			System.out.println(s);		//输出s
    		}
    	}
    

     

  • 相关阅读:
    bzoj2763: [JLOI2011]飞行路线(分层图spfa)
    8.20noip模拟题
    8.19noip模拟题
    1046: [HAOI2007]上升序列(dp)
    bzoj1079: [SCOI2008]着色方案(dp)
    逆序对
    P1966 火柴排队(逆序对)
    NOIP 2015 DAY2
    8.15学校模拟
    差分
  • 原文地址:https://www.cnblogs.com/smxbo/p/10685762.html
Copyright © 2011-2022 走看看