麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

使用Collections

2019-11-18 13:22:27
字體:
來源:轉載
供稿:網友

  下面這張表格總結了用一個集合能做的所有事情(亦可對Set和List做同樣的事情,盡管List還提供了一些額外的功能)。Map不是從Collection繼續的,所以要單獨對待。
  
使用Collections


  boolean add(Object) *保證集合內包含了自變量。假如它沒有添加自變量,就返回false(假)
  boolean addAll(Collection) *添加自變量內的所有元素。假如沒有添加元素,則返回true(真)
  void clear() *刪除集合內的所有元素
  boolean contains(Object) 若集合包含自變量,就返回“真”
  boolean containsAll(Collection) 若集合包含了自變量內的所有元素,就返回“真”
  boolean isEmpty() 若集合內沒有元素,就返回“真”
  Iterator iterator() 返回一個反復器,以用它遍歷集合的各元素
  boolean remove(Object) *如自變量在集合里,就刪除那個元素的一個實例。假如已進行了刪除,就返回“真”
  boolean removeAll(Collection) *刪除自變量里的所有元素。假如已進行了任何刪除,就返回“真”
  boolean retainAll(Collection) *只保留包含在一個自變量里的元素(一個理論的“交集”)。假如已進行了任何改變,就返回“真”
  int size() 返回集合內的元素數量
  Object[] toArray() 返回包含了集合內所有元素的一個數組
  
  *這是一個“可選的”方法,有的集合可能并未實現它。若確實如此,該方法就會碰到一個UnsupportedOperatiionException,即一個“操作不支持”違例,詳見第9章。
  
  下面這個例子向大家演示了所有方法。同樣地,它們只對從集合繼續的東西有效,一個ArrayList作為一種“不常用的分母”使用:
  
  //: Collection1.java
  // Things you can do with all Collections
  package c08.newcollections;
  import java.util.*;
  
  public class Collection1 {
   // Fill with 'size' elements, start
   // counting at 'start':
   public static Collection
   fill(Collection c, int start, int size) {
  for(int i = start; i < start + size; i++)
   c.add(Integer.toString(i));
  return c;
   }
   // Default to a "start" of 0:
   public static Collection
   fill(Collection c, int size) {
  return fill(c, 0, size);
   }
   // Default to 10 elements:
   public static Collection fill(Collection c) {
  return fill(c, 0, 10);
   }
   // Create & upcast to Collection:
   public static Collection newCollection() {
  return fill(new ArrayList());
  // ArrayList is used for simplicity, but it's
  // only seen as a generic Collection
  // everywhere else in the PRogram.
   }
   // Fill a Collection with a range of values:
   public static Collection
   newCollection(int start, int size) {
  return fill(new ArrayList(), start, size);
   }
   // Moving through a List with an iterator:
   public static void print(Collection c) {
  for(Iterator x = c.iterator(); x.hasNext();)
   System.out.print(x.next() + " ");
  System.out.println();
   }  
   public static void main(String[] args) {
  Collection c = newCollection();
  c.add("ten");
  c.add("eleven");
  print(c);
  // Make an array from the List:
  Object[] array = c.toArray();
  // Make a String array from the List:
  String[] str =
   (String[])c.toArray(new String[1]);
  // Find max and min elements; this means
  // different things depending on the way
  // the Comparable interface is implemented:
  System.out.println("Collections.max(c) = " +
   Collections.max(c));
  System.out.println("Collections.min(c) = " +
   Collections.min(c));
  // Add a Collection to another Collection
  c.addAll(newCollection());
  print(c);
  c.remove("3"); // Removes the first one
  print(c);
  c.remove("3"); // Removes the second one
  print(c);
  // Remove all components that are in the
  // argument collection:
  c.removeAll(newCollection());
  print(c);
  c.addAll(newCollection());
  print(c);
  // Is an element in this Collection?
  System.out.println(
   "c.contains(/"4/") = " + c.contains("4"));
  // Is a Collection in this Collection?
  System.out.println(
   "c.containsAll(newCollection()) = " +
   c.containsAll(newCollection()));
  Collection c2 = newCollection(5, 3);
  // Keep all the elements that are in both
  // c and c2 (an intersection of sets):
  c.retainAll(c2);
  print(c);
  // Throw away all the elements in c that
  // also appear in c2:
  c.removeAll(c2);
  System.out.println("c.isEmpty() = " +
   c.isEmpty());
  c = newCollection();
  print(c);
  c.clear(); // Remove all elements
  System.out.println("after c.clear():");
  print(c);
   }
  } ///:~
  
  通過第一個方法,我們可用測試數據填充任何集合。在當前這種情況下,只是將int轉換成String。第二個方法將在本章其余的部分經常采用。
  newCollection()的兩個版本都創建了ArrayList,用于包含不同的數據集,并將它們作為集合對象返回。所以很明顯,除了Collection接口之外,不會再用到其他什么。
  print()方法也會在本節經常用到。由于它用一個反復器(Iterator)在一個集合內遍歷,而任何集合都可以產生這樣的一個反復器,所以它適用于List和Set,也適用于由一個Map生成的Collection。
  main()用簡單的手段顯示出了集合內的所有方法。
  在后續的小節里,我們將比較List,Set和Map的不同實現方案,同時指出在各種情況下哪一種方案應成為首選(帶有星號的那個)。大家會發現這里并未包括一些傳統的類,如Vector,Stack以及Hashtable等。因為不管在什么情況下,新集合內都有自己首選的類。

上一篇:Java中的Hashtable

下一篇:Java通用集合庫

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色片网站免费观看 | 久久精品二区 | 免费观看三级毛片 | 羞羞答答xxdd在线播放 | 国产一级毛片网站 | 国产精品久久久久久久久久久天堂 | 天天干导航| 国产精品白嫩白嫩大学美女 | 欧美性猛交xxxxx按摩国内 | www.精品久久 | 免费久久久久久久 | 99视频观看| 欧美精品日日鲁夜夜添 | 久久久久久久久日本理论电影 | 日日草日日干 | 精品亚洲午夜久久久久91 | 国产精品久久国产精麻豆96堂 | 欧美日韩免费在线观看视频 | av在线免费观看播放 | 91短视频版高清在线观看免费 | 国产一区二区三区四区精 | 国产精品久久久久久久av三级 | 国产精品一区二区三区在线播放 | 久久伊人国产精品 | 午夜视频在线观看91 | 亚洲欧美国产精品va在线观看 | 欧美黑人伦理 | 中国黄色一级生活片 | 天堂成人一区二区三区 | 成人精品一区二区 | 国产精品a一 | 日本特级a一片免费观看 | 亚洲小视频在线 | 免费一级a毛片在线播放视 日日草夜夜操 | 日韩视频一| 国产一区二区精彩视频 | 3344永久免费 | 红桃一区 | 亚洲日本韩国精品 | 亚洲最新无码中文字幕久久 | 色吧久久|