国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

使用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通用集合庫

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新丰县| 含山县| 和硕县| 科尔| 澄江县| 甘孜县| 腾冲县| 东莞市| 哈巴河县| 交城县| 建宁县| 平阴县| 永德县| 高州市| 汤阴县| 潼南县| 德庆县| 泗阳县| 吉木乃县| 枞阳县| 习水县| 鄂托克前旗| 灌南县| 会同县| 晋中市| 绥芬河市| 楚雄市| 伊春市| 洮南市| 仪陇县| 福贡县| 合江县| 宁远县| 扶余县| 呈贡县| 南丰县| 北碚区| 阿瓦提县| 临安市| 永清县| 庆城县|