java當中的類集的架構.(2)(第42集)

來源:互聯網
上載者:User

以下內容來自marschen的java4android視頻教程

  類集架構就是Jdk提供的類與介面.
    本集主要內容.
    
    1. collection和iterator介面
    2. set與hashSet的使用方法

   collection介面
  boolean add 向集合當中加入一個對象
  clear 刪除集合當中的所有對象
  isEmpty       判斷集合是否為空白
  remove        從集合中刪除一個對象的引用
  int size();   返回集合中元素的數目.
  
  collection的一個子介面就是set.
  set介面有一個實作類別,就是hastSet.
   

import java.util.Set;import java.util.HashSet;public class Test{public static void main(String args[]){HashSet<String> hashSet = new HashSet<String>();Set<String> set = hashSet;//Set<Stinrg> set = new HashSet<String>();boolean b1 = set.isEmpty();System.out.println(b1);set.add("a");set.add("b");set.add("c");set.add("d");boolean b2 = set.isEmpty();System.out.println(b2);int i = set.size();System.out.println("remove之前set對象的長度是"  + i);//set.clear();set.remove("a");int j = set.size();System.out.println("remove之後set對象的長度是"  + j);}}

import java.util.Set;import java.util.HashSet;import java.util.Iterator;public class Test1{public static void main(String args[]){//HashSet<String> hashSet = new HashSet<String>();//Set<String> set = hashSet;//Iterator<--Collection<---Set <-----HashSet///                    <---List<----ArrayList//hasNext() next() Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");//調用Set對象的iterator方法會產生一個迭代器對象,該對象用於便溺整個對set.Iterator<String> it = set.iterator();//產生一個迭代器的對象./*boolean b1 = it.hasNext();if(b1){String s = it.next();System.out.println(s);}boolean b2 = it.hasNext();if(b2){String s = it.next();System.out.println(s);}*/while(it.hasNext()){String s = it.next();System.out.println(s);}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.