Java集合类
一 List接口
常用的实现类 ArrayList和 LinkedList。
1)ArrayList: 底层实现的方法时: 动态数组 。
ArrayList、LinkedList比较:
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
2.对于随机访问,ArrayList觉得优于LinkedList。LinkedList需要不断移动指针引用。
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
先看 ArrayList
看源码:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient E[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
ArrayList继承了AbstractList抽象类,实现List 等接口。
elementData:用来存储add方法等添加的数据数组。
size:数组中有效数据的个数。
ArrayList构造方法:
// 由调用者指定集合初始容量,即是elementData数组长度
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
//默认则为 10
public ArrayList() {
this(10);
}
下面是ArrayList的add源码:
//添加数据
public boolean add(E o) {
//确保有足够的容量能存放添加的数据
ensureCapacity(size + 1); // Increments modCount!!
//把数据放入数组中,size+1;
elementData[size++] = o;
return true;
}
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
//当集合需要的最小容量不够时,即minCapacity > oldCapacity时;
//集合中数组长度扩展为1.5倍+1;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
//扩展生成新的数组 elementData
elementData = (E[])new Object[newCapacity];
//将原先的oldData 数组数据 copy至 elementData
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
下面是ArrayList的删除源码:
public E remove(int index) {
//检查集合中是否存在该索引
RangeCheck(index);
modCount++;
E oldValue = elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
//对index索引后面的数据分别向前移动一位。
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
LinkedList 删除方法:
public E remove(int index) {
return remove(entry(index));
}
//删除方法,只需要将删除元素左右两边的引用关联上即可。
private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}
private E remove(Entry<E> e) {
if (e == header)
throw new NoSuchElementException();
E result = e.element;
//关联删除节点左右两边的节点。
//如上图:若第一个节点被删除,则将header与第二个节点关联上。形成新链表。
e.previous.next = e.next;
e.next.previous = e.previous;
e.next = e.previous = null;
e.element = null;
size--;
modCount++;
return result;
}
分享到:
相关推荐
day14-ArrayList集合 1.ArrayList 1.1ArrayList类概述【理解】 什么是集合 提供一种存储空间可变的存储模型,存储的数据容量可以发生改变 ArrayList集合的特点 底层是数组实现的,长度可以变化 泛型的使用 ...
set list ArrayList等java集合类详述
java集合类ArrayList简单雇员管理系统.doc
java集合类ArrayList总结共9页.pdf.zip
java集合类arraylist循环中删除特定元素的方法.docx
掌握List集合、Set集合、Map集合的使用以及Iterator迭代器和foreach循环的使用 了解常用的集合类 熟悉泛型的使用
java 集合源码 JDK-Collection集合入门 总的list和set类结构大致如下所示 Map不继承Collection,其结构如下 首先介绍下迭代器的概念 迭代器无非是一个接口,假设我们有一个数组,如果我们要实现迭代器,只需要根据该接口...
超全Java集合框架讲解 - 超全Java集合框架讲解 - 集合框架总览 - Iterator Iterable ListIterator - Map 和 Collection 接口 - Map 集合体系详解 - HashMap - LinkedHashMap - TreeMap - WeakHashMap - ...
2.学生管理系统 1. 定义学生类,包含以下成员变量 2. 学生管理系统主界面的搭建步骤 3. 学生管理系统的添加学生功能实现步骤
java集合类 Java中有哪些容器(集合类)? 线程安全和线程不安全的分别有哪些? Map接口有哪些实现类? 描述一下Map put的过程 如何得到一个线程安全的Map? HashMap有什么特点? ConcurrentHashMap是怎么分段分组...
我写的关于set集合和list集合相关性能测试,linkedList ArrayList HashSet 等类的增删改查性能测试
java.util 中的集合类包含 Java 中某些最常用的类。 最常用的集合类是 List 和 Map。 List 的具体实现包括 ArrayList 和 Vector,它们是可变大小的列表,比较适合构建、存储和操作任何类型对象的元素列表。 List ...
下面小编就为大家带来一篇Java集合类ArrayList循环中删除特定元素的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
3)了解List接口及主要实现类(ArrayList、LinkedList、Vector) 4)了解Map接口及主要实现类(HashMap、TreeMap、HashTable) 二、实验内容及步骤 1、编写程序练习将以下5个Person类的对象放在一个HashSet中。 姓名...
java代码-使用集合ArrayList对字符串进行存储和管理
集合类的使用如ArrayList,HashMap ◦对这些集合类的操作包括:遍历、添加、排序,得到集合的大小、判断集合里是否已存在指定元素 第三是熟悉对象的序列化和反序列化 作为类的方法来实现下面二个方法 ◦public ...
在Java中,CopyOnWriteArrayList(写入时复制数组列表)是线程安全的集合类,它实现了List接口,并使用了"写入时复制"的策略来保证线程安全性。 CopyOnWriteArrayList的主要特点是:在进行修改操作(例如添加、修改...
JAVA集合类的一些总结 collection,vector,arraylist,list,hashtable...
Java类集框架 最大单值操作父接口Collection及其子接口、子类: List接口、ArrayList类、Vector类、栈操作类Stack、链表操作类LinkList、队列操作接口Queue、Set接口、HashSet类、TreeSet类、SortedSet接口 双值操作...