java
来源:互联网 发布:三星专用下载软件 编辑:程序博客网 时间:2024/06/03 17:28
java学习之路的学习记录
java几个常见的具体集合详解
List 接口 用于描述一个有序集合。并且集合中的每个元素的位置都十分重要。实现类LinkedList , ArrayList
链表 LinkedList
在java中,所有链表都是双向的。链表可以实现快速的添加跟删除。但是链表是一个有序集合,那么问题来了,链表的add()方法每次都是只能添加到链表的尾部,见api几个重要的方法。
boolean add(E e)//Appends the specified element to the end of this list.void add(int index, E element)//Inserts the specified element at the specified position in this list.在给定的位置添加一个元素ListIterator<E> listIterator(int index)//Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. 返回一个实现了ListIterator接口的迭代器对象
很多的时候,需要给链表的表中添加元素,由于迭代器是描述集合中的位置,所以可以调用迭代器中的add()方法,来为链表添加元素。而且只有对自然有序的集合使用迭代器添加元素才有实际意义。
Interface ListIterator 中方法
void add(E e)//Inserts the specified element into the list (optional operation).boolean hasNext()//Returns true if this list iterator has more elements when traversing the list in the forward direction.boolean hasPrevious()//Returns true if this list iterator has more elements when traversing the list in the reverse direction.//E next()Returns the next element in the list and advances the cursor position.int nextIndex()//Returns the index of the element that would be returned by a subsequent call to next().E previous()//Returns the previous element in the list and moves the cursor position backwards.int previousIndex()//Returns the index of the element that would be returned by a subsequent call to previous().void remove()//Removes from the list the last element that was returned by next() or previous() (optional operation).void set(E e)//Replaces the last element returned by next() or previous() with the specified element (optional operation).
例子
List<String> staff = new LinkedList<>(); staff.add("john"); staff.add("tom"); staff.add("lucy"); ListIterator<String> iter = staff.listiterator(); iter.next(); iter.add("lily");//将lily添加到iter的位置,就是john后面
还有一个问题:
对于同一个集合,如果有两个迭代器,一个在修改集合的时候,另外一个在访问,这样就会出现抛出异常。怎么解决呢?
方法1:可以根据需要给容器附加很多迭代器,但是这些迭代器只能读取列表,另外,再单独附加一个迭代器即可以读也可以写。
方法2:集合可以跟踪改写操作的次数。每个迭代器都维护一个独立的计数器。在每个迭代器开始操作之前都检查一下自己改写的操作的数值是不是跟集合的计数器的数值一致。如果不一致就抛出异常。
数组列表 ArrayList
ArrayList封装了一个动态再分配的对象数组。动态的数组,长度可以自动变化。支持随机访问效率高。但是插入跟删除元素的效率低。需要大量移动元素。
ArrayList 与 Vector的区别
http://www.cnblogs.com/wanlipeng/archive/2010/10/21/1857791.html
http://www.cnblogs.com/muzongyan/articles/1782788.html
集 Set
散列集 HashSet
树集 TreeSet
树集是一个有序集合,可以按照顺序将元素插入到集合中。但是在对集合遍历时候,每个值将自动的按照排序后的顺序输出。
映射 Map
知识点:
散列映射 HashMap
树映射 TreeMap
弱散列映射 WeakHashMap
链接散列集 LinkedHashSet
链接散列映射 LinkedHashMap
枚举集 EnumSet
枚举映射 EnumMap
标识散列映射 IdentityHashMap
知识点
1 映射视图
2 视图与包装器
见 java - 集合框架视图与包装器 (要整理!未完待续。。。)
- java
- JAVA
- JAVA
- JAVA
- java
- Java
- Java
- JAVA:
- java
- java
- java
- java
- Java
- java
- java
- java
- JAVA?
- java
- 通过河内之塔问题深入理解递归思想
- Gym 101246E Kidnapping 递推or dfs 优化
- 线程池为什么能维持线程不释放,随时运行各种任务?
- Adreno SDK 详细介绍(一)
- 设计模式---适配器模式
- java
- java基础知识8继承
- bzoj4820
- Java最最基础的知识(适合初学者看)
- spring boot +jquery mobile构建web APP
- STL中map的简单使用
- 红帽vsftpd 文件共享服务
- hdu 5544 Ba Gua Zhen(线性基+dfs)
- 初识Qualcomm Adreno SDK