网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  使用lists     
  文章作者:未知  文章来源:水木森林  
  查看:106次  录入:管理员--2007-11-17  
 
  
使用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()中,只是简单地发出调用,以便揭示出正确的语法。而且尽管捕获了返回值,但是并未使用它。在某些情况下,之所以不捕获返回值,是由于它们没有什么特别的用处。在正式使用它们前,应仔细研究一下自己的联机文档,掌握这些方法完整、正确的用法。
 
 
上一篇: 使用sets    下一篇: 您的 利用 java 平台的特性建造一个令人瞩目的系统
  相关文档
setutcseconds 方法 11-16
Java入门:java类文件详解 03-25
群组通讯工具jgroups 2.2.9 final发布 11-17
java入门:初学者因该了解内存优化编程 12-28
看 junit 中metedata的巧妙应用 11-17
如何使用preparedstatement减少开发时间 11-20
简单直观-实战体会java多线程编程精要 02-03
jdbc专题介绍(2) 11-17
java编程中更新xml文档的常用方法介绍 11-16
用java实现回调例程 11-16
beans入门必读之无状态会话bean基础 11-17
java gui误解 因为外观而拒绝swing? 11-16
图片的预先读取-加快下载速度 11-17
服务器及中间件:apache mina 线程模型配置 11-16
利用java做一个简单的计算器 04-14
java 2 引用类使用指南 11-17
类和对象的兼容性 11-17
往jdk里加入一个安全提供者以获取算法 11-16
六个 for 循环的java小程序源码展播 11-17
在jdk1.4中使用java 5的语言特性 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息