接上篇  java集合类(一)

  1. List接口继承了Collection接口和Iterable接口,即同样含有Collection和 Iterable的特性,还有方法,其基本方法有:

1)有关添加: boolean add(E e):添加元素   void add(int index,E element):在特定位置添加元素

boolean addAll(Collection<? extends E> c):添加集合中所有的元素    boolean addAll(int index,Collection<? extends E> c):在特定位置添加一组元素

2)有关清除:void clear(), E remove(int index),boolean remove(Object o),boolean remove(Collection<?> c)

3)有关检验:boolean contains(Object o),boolean containAll(Collection<?> c),boolean equals(Object o),boolean isEmpty()

4)有关获取:E get(int index),int hashCode(),int indexOf(Object o),int lastIndexOf(Object o),int size(),Lisy<E> subList(int fromindex,int toindex)

5)有关设定:E set(int index, E element),boolean retainAll(Collection<?> c)

6)有关打印:Object[] toArray(),    <T>T[] toArray(T[] a)

7)有关迭代:Iterator<E> iterator(),ListIterator<E> listIterator(),ListIterator<E> listIterator(int index)

8)其他:int hashcode()

List接口的实现类有很多,如:AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList,

RoleUnresolvedList,  Stack, Vector,下面介绍List的几个基本实现类的用法:

  • About ArrayList:

All Implemented Interfaces:  Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess extends AbstractList

    Direct Known Subclasses:  AttributeList, RoleList, RoleUnresolvedList

实例代码:

  1. class iphone{}
  2.  
  3. public void arraylist(){
  4. //1)Constructs an empty list with an initial capacity of ten
  5. ArrayList al = new ArrayList();
  6. /*
  7. * 2)Constructs a list containing the elements of the specified
  8. * collection, in the order they are returned by the collection's iterator
  9. */
  10. ArrayList<Integer> alist = new ArrayList(Arrays.asList(1,2,3));
  11. //3)Constructs an empty list with the specified initial capacity
  12. ArrayList<Double> alist1 = new ArrayList(10);
  13. al.add(new iphone());
  14. al.add(1);
  15. al.add("a");
  16. al.add(alist);
  17. System.out.println(al.hashCode()); //int hashcode()
  18.  
  19. /*
  20. * usage of methods that aren't mentioned within "List basic methods"
  21. */
  22. //ensure that list can hold at least the number of elements specified by the minimum capacity argument
  23. alist1.ensureCapacity(30);
  24. //Trims the capacity of this ArrayList instance to be the list's current size
  25. alist.trimToSize();
  26.  
  27. //Returns a shallow copy of this ArrayList instance(The elements themselves are not copied)
  28. Object o = al.clone();
  29. }
  • About LinkedList:  

All Implemented Interfaces:Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>  extends AbstractSequentialList<E>

从上面可以知道,由于LinkedList实现的接口涉及到队列,所以它也会新增一些有关队列独有操作方法,还有pop(),push()等,下面举例:

  1. public void linkedlist(){
  2. LinkedList linklist = new LinkedList();
  3. ArrayList<Integer> a = new ArrayList(Arrays.asList(1,2,3));
  4. LinkedList linklist2 = new LinkedList(a);
  5.  
  6. Iterator i = linklist2.iterator();
  7. while(i.hasNext()){
  8. System.out.println(i.next()); //1 2 3
  9. }
  10. Iterator ri = linklist2.descendingIterator();
  11. while(ri.hasNext()){
  12. System.out.println(ri.next()); //3 2 1
  13. }
  14.  
  15. linklist.addFirst(1);
  16. linklist.addLast("e");
  17. System.out.println(linklist); //[1,e]
  18. /*linklist.getFirst();
  19. linklist.getLast();*/
  20.  
  21. //Retrieves, but does not remove, the head (first element) of this list
  22. System.out.println(linklist.element()); //1
  23.  
  24. linklist.offer("x");
  25. System.out.println(linklist);//[1,e,x]
  26. linklist.offerFirst(0);
  27. linklist.offerLast("y");
  28. System.out.println(linklist);//[0,1,e,x,y]
  29.  
  30. //peek..()--->[Retrieves, but does not remove]
  31. System.out.println(linklist.peek());//0
  32. System.out.println(linklist.peekFirst());//0 ,list is empty,return "null"
  33. System.out.println(linklist.peekLast());//y,list is empty, return "null"
  34.  
  35. //poll..()--->[Retrieves and removes]
  36. System.out.println(linklist.poll()); //0
  37. System.out.println(linklist.pollFirst());//1
  38. System.out.println(linklist.pollLast()); //y
  39. System.out.println(linklist);//[e,x]
  40.  
  41. /*linklist.removeFirst(); //return first element
  42. linklist.removeFirstOccurrence(Object); //boolean
  43. linklist.removeLast(); //return last element
  44. linklist.removeLastOccurrence(Object); //boolean
  45. */
  46.  
  47. System.out.println(linklist2); //[1 2 3]
  48. //Pops an element from the stack represented by this list
  49. System.out.println(linklist2.pop()); //1
  50. //Pushes an element onto the stack represented by this list
  51. linklist2.push("m");
  52. System.out.println(linklist2);//[m,2,3]
  53. }
  • ArrayList 和LinkedList的比较:

 1.ArrayList是基于动态数组,LinkedList基于链表
       2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针
       3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据

验证:

  1. public void comparearraylistwithlinklist(){
  2. SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  3. System.out.println(f.format(new Date()));
  4.  
  5. ArrayList<Integer> a = new ArrayList<Integer>();
  6. long starttime = new Date().getTime();
  7. for(int i = 0; i<10000; i++){
  8. a.add(i);
  9. }
  10. long finishtime = new Date().getTime();
  11. System.out.println(finishtime-starttime);
  12.  
  13. LinkedList<Integer> l = new LinkedList<Integer>();
  14. long lstarttime = new Date().getTime();
  15. for(int i = 0; i<10000; i++){
  16. l.add(i);
  17. }
  18. long lfinishtime = new Date().getTime();
  19. System.out.println(lfinishtime-lstarttime);
  20. }

  输出:

额外说明:以上验证代码是基于较大量数据的,输出也是不稳定的,即答案也不能确定,可能是我用错测试方法,也可能是因为数据量不够大,也可能是因为getTime()获得的是毫秒,程序可能需要更精确的时间单位,这样才有办法比较。另外,如果对于单个数据的插入或删除,是不是LinkedList还优于ArrayList呢?答案也很明显是不一定的,读者可以按照上面的实例验证一下

由于此博文可能有点长了,其他List的学习见“java集合类(三)List学习(续)”,尽请期待!!

###    学习从来都是一个过程,对对错错对对...若文中有错误,还望读者批评指出      ###

java集合类(二)List学习的更多相关文章

  1. java集合类源码学习一

    对于java的集合类,首先看张图 这张图大致描绘出了java集合类的总览,两个体系,一个Collection集合体系一个Map集合体系.在说集合类之前,先说说Iterable这个接口,这个接口在jdk ...

  2. java集合类源码学习三——ArrayList

    ArrayList无疑是java集合类中的一个巨头,而且或许是使用最多的集合类.ArrayList继承自AbstractList抽象类,实现了List<E>, RandomAccess, ...

  3. java集合类源码学习二

    我们查看Collection接口的hierarchy时候,可以看到AbstractCollection<E>这样一个抽象类,它实现了Collection接口的部分方法,Collection ...

  4. java集合类(二)

    第十六天知识点总结 一.泛型 泛型:java jdk 1.5 新特性. 泛型的好处: 1.运行时的错误提前到编译时. 2.避免无谓的强制类型转换 自定义方法泛型:自定义泛型就是一个数据类型的占位或一个 ...

  5. Java集合类源码学习- Iterabel<T>,Colection<E>,AbstractCollection<E>

    Collection<E>接口extends Iteratable<E>接口. Iteratable<T>:实现接口使得对象能够成为“for-each loop”的 ...

  6. Java集合类综合

    Java集合类是JDK学习中的一个经典切入点,也是让初学者最初感受到Java魅力的地方之一,你一定不会忘记不需要关心大小的ArrayList,不用自己实现的Queue,和随处可见的HashMap.面试 ...

  7. 201871010123-吴丽丽《面向对象程序设计(Java)》第十二周学习总结

    201871010123-吴丽丽<面向对象程序设计(Java)>第十二周学习总结 项目 内容 这个作业属于哪个课程  https://www.cnblogs.com/nwnu-daizh/ ...

  8. 201871010106-丁宣元 《面向对象程序设计(java)》第十二周学习总结

    201871010106-丁宣元 <面向对象程序设计(java)>第十二周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...

  9. 201271050130-滕江南-《面向对象程序设计(java)》第十二周学习总结

    201271050130-滕江南-<面向对象程序设计(java)>第十二周学习总结 项       目 内      容 这个作业属于哪个课程 https://www.cnblogs.co ...

随机推荐

  1. MySQLdb模块安装-win环境

    原帖地址:http://blog.csdn.net/wklken/article/details/7253245 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见  P ...

  2. Part 1 What is AngularJS

    What is AngularJS ? AngularJS is a JavaScirpt framework that helps build web application. AngularJS ...

  3. php 读写xml 修改xml

    需要修改的xml <?xml version="1.0"?> <config> <administration> <adminuser&g ...

  4. php数组中删除元素之重新索引

    如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊 <?php $arr = array('a','b','c','d'); unset($arr[1]); ...

  5. Apache windows多线程设置

    # WinNT MPM # ThreadsPerChild: constant number of worker threads in the server process # MaxRequests ...

  6. 【学习笔记】【C语言】赋值运算

    将某一数值赋给某个变量的过程,称为赋值. 1. 简单赋值 C语言规定,变量要先定义才能使用,也可以将定义和赋值在同一个语句中进行 int a = 10 + 5;的运算过程 a = b = 10;的运算 ...

  7. 用NOPI将图片二进制流导出到Excel

    这儿采取的是将图片的二进制流导出到Excel,直接上代码: /// <summary> /// DataTable导出到Excel的MemoryStream /// </summar ...

  8. 使用Eclipse maven构建springmvc项目

    Eclipse maven构建springmvc项目 Listener 监听器 架构 使用Log4J监控系统日志邮件警报 2014-12-16 13:09:16 控制器在完成逻辑处理后,通常会产生一些 ...

  9. 一些常用css技巧的为什么(二)我所理解的line-height

    要用到的基本术语和概念: 替换元素:用作为其他内容占位符的一个元素,或说替换元素内容的部分并非由文档内容直接表示.比如img元素它由文档本身之外的一个图像来替换,比如input元素要由一个单选按钮,复 ...

  10. 卸载Linux内置的AMP软件

    卸载Linux内置的AMP软件 在安装Linux软件的LAMP环境时,必须有一个前提:必须要完全卸载掉系统内置的AMP软件. 1.卸载httpd软件(Apache) 如果在卸载软件时出现依赖关系,我们 ...