java collections读书笔记(11) Lists
继续这个系列,好久没学习了,懒惰呀。
Set接口,实际上是collection 类别中最简单的一个接口,因为它并没有比Collection 接口增加任何的内容,相对而言,大家可能更喜欢List接口和它的扩展:ArrayList和LinkedList。
List加入了位置属性,从而,可以有多种的排序可能。因此,除了可以使用Iterator获取元素之外,还可以使用ListIterator<E> ,通过index,获取指定位置的元素。
在新的框架里,ArrayList,是用来替代vector的。
ArrayList is the replacement for Vector in the new framework. It represents an
unsynchronized vector where the backing store is a dynamically growable array. On the other hand is
LinkedList, which, as its name implies, is backed by the familiar linked list data structure.
ArrayList:
显然,ArrayList是替代Vector的,相对Vector而言,ArrayList是非同步的。另外,如果想快速的,随机的访问其中的元素,ArrayList是可以的(前提是不要有大量频繁的元素插入和删除等操作,否则,考虑LinkedList)。
ArrayList是扩展自AbstractList.而且,支持重复的元素。
1)ArrayList的初始化
主要是这么几个构造函数:
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/*newCapacity= (oldCapacity * 3)/2 + 1.*/
public ArrayList() {
this(10);
}
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
一个容易犯错的例子:
String elements[] = {"Schindler's List", "Waiting List", "Shopping List",
"Wine List"};
List list = new ArrayList(Arrays.asList(elements));
这个时候,aslist返回的arraylist,是arrays的内部类,不是java.util下的arraylist,具体见:
http://www.cnblogs.com/hashmap/articles/2162444.html
2)增加元素
public boolean add(Object element)
public boolean add(int index, Object element)
3)删除元素
ArrayList mylist=new ArrayList();
mylist.add("wcf1");
mylist.add("wcf2");
mylist.add("wcf3");
mylist.add("wcf4");
mylist.add("wcf5");
mylist.add("wcf6");
System.out.println(mylist);
mylist.add("wcf3");
System.out.println(mylist);
mylist.remove("wcf3");
System.out.println(mylist);
[wcf1, wcf2, wcf3, wcf4, wcf5, wcf6]
[wcf1, wcf2, wcf3, wcf4, wcf5, wcf6, wcf3]
[wcf1, wcf2, wcf4, wcf5, wcf6, wcf3]
4)列举
public Iterator iterator()
public ListIterator listIterator()
public ListIterator listIterator(int index)
因为list是排序的,因此,iterator和listIterator的操作结果是一样的。
他们的区别,见:http://www.cnblogs.com/aomi/p/3166292.html
LinkedList
ArrayList和LinkedList在作为List接口 使用的时候,主要区别就是随机访问(按照index进行查找删除等等操作)效率,ArrayList的随机访问效率当然会高;如果你的程序很少应用随机访 问,那使用LinkedList不会是比ArrayList更差的选择。ArrayList最没有效率的地方就是频繁的添加操作,这个操作可能会引起 ArrayList的扩容,扩容的时候会copy数组浪费点时间,而LinkedList没有扩容时的问题。
还有一个地方不同:LinkedList实现了Deque接口,如果我们需要队列操作,那声明LinkedList的实现为Deque类型是非常方便的,ArrayList没有实现这个接口。
这是个链表结构。
ListIterator
a)来自Iterator
ListIterator iter = list.listIterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
b)可以倒序
ListIterator iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
System.out.println(iter.previous());
}
java collections读书笔记(11) Lists的更多相关文章
- 《Effective Java》读书笔记 - 11.序列化
Chapter 11 Serialization Item 74: Implement Serializable judiciously 让一个类的实例可以被序列化不仅仅是在类的声明中加上" ...
- java collections读书笔记(10) Set
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVgAAADbCAIAAACnXR7VAAAgAElEQVR4nOx9d1hVV9Y3880zb2YmM3 ...
- java collections读书笔记(8)collection框架总览(1)
- java collections读书笔记(9)collection框架总览(2)
框架算法: 1)collection接口 add() Adds an element to the collection.addAll() Adds a collection of element ...
- Java并发读书笔记:线程安全与互斥同步
目录 导致线程不安全的原因 什么是线程安全 不可变 绝对线程安全 相对线程安全 线程兼容 线程对立 互斥同步实现线程安全 synchronized内置锁 锁即对象 是否要释放锁 实现原理 啥是重进入? ...
- 机器学习实战 - 读书笔记(11) - 使用Apriori算法进行关联分析
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第11章 - 使用Apriori算法进行关联分析. 基本概念 关联分析(associat ...
- java effective 读书笔记
java effective 读书笔记 []创建和销毁对象 静态工厂方法 就是“封装了底层 暴露出一个访问接口 ” 门面模式 多参数时 用构建器,就是用个内部类 再让内部类提供构造好的对象 枚举 si ...
- 深入理解Java虚拟机 -- 读书笔记(1):JVM运行时数据区域
深入理解Java虚拟机 -- 读书笔记:JVM运行时数据区域 本文转载:http://blog.csdn.net/jubincn/article/details/8607790 本系列为<深入理 ...
- 强化学习读书笔记 - 11 - off-policy的近似方法
强化学习读书笔记 - 11 - off-policy的近似方法 学习笔记: Reinforcement Learning: An Introduction, Richard S. Sutton and ...
随机推荐
- window下mysql的1053错误
tmp文件数目过多 导致启动为1053错误 这个时候有两种方法 清空tmp目录 更改mysql的tmp目录 在my.ini中更改 [mysqld] tmpdir="d:/mysql_tmp& ...
- 批量更改int类型的timestamp字段to datetime
批量更改int类型的timestamp字段to datetime 1.创建datetime字段created_at 2.update 字段 UPDATE table set created_at = ...
- mysql默认字符集修改
(1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值,添加 [mysql] default-character-set = utf8 [mysqld] character_se ...
- the basic index concept
Computer Science An Overview _J. Glenn Brookshear _11th Edition Over the years numerous variations o ...
- base64coder调用
base64coder 可以查看官网: http://www.source-code.biz/base64coder/java/ 我所涉及到的 base64coder调用: 某天,因需要修改Pr ...
- 深入理解Javascript闭包 新手版
一.什么是闭包? “官方”的解释是:所谓“闭包”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 相信很少有人能直接看懂这句话,因为他描述 ...
- JBoss的安装与配置(对应eclipse配置)【转】
安装JBoss纯粹是目的就是学习EJB3...至少现在是这样的 EJB需要运行在EJB容器中.每个J2EE应用服务器都含有EJB容器和Web容器.这样,既支持运行EJB,也可以运行Web应用 目前EJ ...
- java开发bug 在启动Tomcat 6.0时发现第一条信息便是
MyEclipse 8.5 + tomcat6 + jdk 1.8 启动的时候报错: The APR based Apache Tomcat Native library which allows o ...
- CLR调试报错“Visual Studio远程调试监视器 (MSVSMON.EXE) 的 64 位版本无法调试 32 位进程或 32 位转储。请改用 32 位版本”的解决
Win7 64位电脑上进行visual studio的数据库项目的CLR存储过程进行调试时,报错: ---------------------------Microsoft Visual Studio ...
- mysql storage enginees
这段时间在看<High Performance MySQL>,看到存储引擎这个地方感到很多细节比较陌生,所以总结小记一些 为 了适应各种不同的运行环境,MYSQL提供了多种不同的存储引擎( ...