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

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

使用Lists

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

  
使用Lists


  List(接口) 順序是List最重要的特性;它可保證元素按照規定的順序排列。List為Collection添加了大量方法,以便我們在List中部插入和刪除元素(只推薦對LinkedList這樣做)。List也會生成一個ListIterator(列表反復器),利用它可在一個列表里朝兩個方向遍歷,同時插入和刪除位于列表中部的元素(同樣地,只建議對LinkedList這樣做)
  ArrayList* 由一個數組后推得到的List。作為一個常規用途的對象容器使用,用于替換原先的Vector。答應我們快速訪問元素,但在從列表中部插入和刪除元素時,速度卻嫌稍慢。一般只應該用ListIterator對一個ArrayList進行向前和向后遍歷,不要用它刪除和插入元素;與LinkedList相比,它的效率要低許多
  LinkedList 提供優化的順序訪問性能,同時可以高效率地在列表中部進行插入和刪除操作。但在進行隨機訪問時,速度卻相當慢,此時應換用ArrayList。也提供了addFirst(),addLast(),getFirst(),getLast(),removeFirst()以及removeLast()(未在任何接口或基礎類中定義),以便將其作為一個規格、隊列以及一個雙向隊列使用
  
  下面這個例子中的方法每個都覆蓋了一組不同的行為:每個列表都能做的事情(basicTest()),通過一個反復器遍歷(iterMotion())、用一個反復器改變某些東西(iterManipulation())、體驗列表處理的效果(testVisual())以及只有LinkedList才能做的事情等:
  
  //: List1.java
  // Things you can do with Lists
  package c08.newcollections;
  import java.util.*;
  
  public class List1 {
   // Wrap Collection1.fill() for convenience:
   public static List fill(List a) {
  return (List)Collection1.fill(a);
   }
   // You can use an Iterator, just as with a
   // Collection, but you can also use random
   // access with get():
   public static void PRint(List a) {
  for(int i = 0; i < a.size(); i++)
   System.out.print(a.get(i) + " ");
  System.out.println();
   }
   static boolean b;
   static Object o;
   static int i;
   static Iterator it;
   static ListIterator lit;
   public static void basicTest(List a) {
  a.add(1, "x"); // Add at location 1
  a.add("x"); // Add at end
  // Add a collection:
  a.addAll(fill(new ArrayList()));
  // Add a collection starting at location 3:
  a.addAll(3, fill(new ArrayList()));
  b = a.contains("1"); // Is it in there?
  // Is the entire collection in there?
  b = a.containsAll(fill(new ArrayList()));
  // Lists allow random access, which is cheap
  // for ArrayList, eXPensive for LinkedList:
  o = a.get(1); // Get object at location 1
  i = a.indexOf("1"); // Tell index of object
  // indexOf, starting search at location 2:
  i = a.indexOf("1", 2);
  b = a.isEmpty(); // Any elements inside?
  it = a.iterator(); // Ordinary Iterator
  lit = a.listIterator(); // ListIterator
  lit = a.listIterator(3); // Start at loc 3
  i = a.lastIndexOf("1"); // Last match
  i = a.lastIndexOf("1", 2); // ...after loc 2
  a.remove(1); // Remove location 1
  a.remove("3"); // Remove this object
  a.set(1, "y"); // Set location 1 to "y"
  // Keep everything that's in the argument
  // (the intersection of the two sets):
  a.retainAll(fill(new ArrayList()));
  // Remove elements in this range:
  a.removeRange(0, 2);
  // Remove everything that's in the argument:
  a.removeAll(fill(new ArrayList()));
  i = a.size(); // How big is it?
  a.clear(); // Remove all elements
   }
   public static void iterMotion(List a) {
  ListIterator it = a.listIterator();
  b = it.hasNext();
  b = it.hasprevious();
  o = it.next();
  i = it.nextIndex();
  o = it.previous();
  i = it.previousIndex();
   }
   public static void iterManipulation(List a) {
  ListIterator it = a.listIterator();
  it.add("47");
  // Must move to an element after add():
  it.next();
  // Remove the element that was just prodUCed:
  it.remove();
  // Must move to an element after remove():
  it.next();
  // Change the element that was just produced:
  it.set("47");
   }
   public static void testVisual(List a) {
  print(a);
  List b = new ArrayList();
  fill(b);
  System.out.print("b = ");
  print(b);
  a.addAll(b);
  a.addAll(fill(new ArrayList()));
  print(a);
  // Shrink the list by removing all the
  // elements beyond the first 1/2 of the list
  System.out.println(a.size());
  System.out.println(a.size()/2);
  a.removeRange(a.size()/2, a.size()/2 + 2);
  print(a);
  // Insert, remove, and replace elements
  // using a ListIterator:
  ListIterator x = a.listIterator(a.size()/2);
  x.add("one");
  print(a);
  System.out.println(x.next());
  x.remove();
  System.out.println(x.next());
  x.set("47");
  print(a);
  // Traverse the list backwards:
  x = a.listIterator(a.size());
  while(x.hasPrevious())
   System.out.print(x.previous() + " ");
  System.out.println();
  System.out.println("testVisual finished");
   }
   // There are some things that only
   // LinkedLists can do:
   public static void testLinkedList() {
  LinkedList ll = new LinkedList();
  Collection1.fill(ll, 5);
  print(ll);
  // Treat it like a stack, pushing:
  ll.addFirst("one");
  ll.addFirst("two");
  print(ll);
  // Like "peeking" at the top of a stack:
  System.out.println(ll.getFirst());
  // Like popping a stack:
  System.out.println(ll.removeFirst());
  System.out.println(ll.removeFirst());
  // Treat it like a queue, pulling elements
  // off the tail end:
  System.out.println(ll.removeLast());
  // With the above Operations, it's a dequeue!
  print(ll);
   }
   public static void main(String args[]) {
  // Make and fill a new list each time:
  basicTest(fill(new LinkedList()));
  basicTest(fill(new ArrayList()));
  iterMotion(fill(new LinkedList()));
  iterMotion(fill(new ArrayList()));
  iterManipulation(fill(new LinkedList()));
  iterManipulation(fill(new ArrayList()));
  testVisual(fill(new LinkedList()));
  testLinkedList();
   }
  } ///:~
  
  在basicTest()和iterMotiion()中,只是簡單地發出調用,以便揭示出正確的語法。而且盡管捕捉了返回值,但是并未使用它。在某些情況下,之所以不捕捉返回值,是由于它們沒有什么非凡的用處。在正式使用它們前,應仔細研究一下自己的聯機文檔,把握這些方法完整、正確的用法。

上一篇:Java通用集合庫

下一篇:使用Sets

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天天躁狠狠躁夜躁2020挡不住 | 久久色播| 亚洲一级电影在线观看 | 欧美激情在线播放 | 中文字幕在线视频日本 | 一级免费黄色免费片 | 亚洲视频观看 | 亚洲一区二区三区视频 | 国产精品视频久 | 欧美三级欧美成人高清www | 国产一区二区精品91 | 久草在线播放视频 | 99视频在线观看视频 | 亚洲一区二区 | 免费毛片视频播放 | 国产成人精品一区二区三区电影 | 久草视频在线资源 | 欧美一级特黄a | 亚洲人成电影在线 | 亚洲免费看片网站 | 在线观看国产一区二区 | 巨根插入 | 国产精品一区二av18款 | 国产精品爆操 | 亚洲va国产va | 久久av免费观看 | v11av在线视频成人 | 一级外国毛片 | 久草在线观看资源 | 成人毛片在线 | 日日噜噜噜夜夜狠狠久久蜜桃 | 国产精品久久久久久久久久 | 亚洲爱爱网站 | 免费av网站观看 | 激情av在线 | 欧美性生活xxxxx | 黄网站免费入口 | 毛片一级片 | 久久久久免费精品国产小说色大师 | www.99热视频 | 深夜福利视频免费观看 |