java集合架構初探

來源:互聯網
上載者:User

標籤:iter   dem   集合   示範   class   span   java集合   als   abc   

Collection介面中的共性功能
1.添加
boolean add(object obj);一次添加一個
boolean addAll(Collection);將指定容器中的所有元素添加

2.刪除
void clear();
boolean removbe(object o);
boolean removeAll(Collection c);
3.擷取長度
int size()
4.判斷
boolean isEmpty();
boolean contains(object o)
boolean contains(object)
5.將集合轉成數組
toArray();
toArray([]);

5.取出集合元素iterator
Iterator iterator()
擷取集合中元素上迭代功能的迭代器對象
迭代:取出元素的一種方式
迭代器:具備著迭代功能的對象
而迭代器對象不需要new 直接通過 iterator()擷取即可。

迭代器是取出collection集合元素中的公用方法

 

package com.runoob.test;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo {    public static void main(String[] args) {        /*         * 示範Collection中的準系統         * */        Collection c1=new ArrayList();        Collection c2=new ArrayList();        /*         * 添加單個元素(對象)         */        c1.add("abc1");        c1.add("abc2");        c1.add("abc3");                c2.add("abc1");        c2.add("abc2");        c2.add("abc7");        //        System.out.println("c1 contains \"abc1\"="+c1.contains("abc1"));//結果為true//        System.out.println("c1 contains \"abc\"="+c1.contains("abc"));//結果為false//        System.out.println(c1.remove("abc1"));//結果為true,代表刪除成功//        System.out.println(c1.remove("abc"));//結果為false,代表刪除失敗        //        c1.clear();//清空c1//        System.out.println(c1);//結果為[]//        System.out.println(c1.isEmpty());結果為true        //        c1.remove("abc1");//        System.out.println(c1);//[abc2,abc3]                int a=c1.size();        System.out.println(a);        //======帶all的方法=======        //        c1.removeAll(c2);//        System.out.println(c1);//結果為[abc3],刪除c1中和c2相同的部分        //        c1.retainAll(c2);//        System.out.println(c1);//結果為[abc1,abc2],返回c1和c2的交集        //    //        c1.addAll(c2);//        System.out.println(c1);//結果為[abc1, abc2, abc3, abc1, abc2, abc7]//        boolean b=c1.containsAll(c2);//        System.out.println(b);//結果為true                                    }}

 

java集合架構初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.