链表之ArrayList
来源:互联网 发布:扬州大学怎么样 知乎 编辑:程序博客网 时间:2024/05/16 01:27
链表是最经常用的数据结构之一,可以是栈和队列的实现的基础。下面直接上代码:
a)modCount 在AbstractList中定义。用来表示链表改变的次数。
b)注意 RangeCheck函数。
c)这里没提及Iterator类,其中iterator类在AbtractList中有一个内部类Itr来表示,用iterator()来引入
public Iterator<E> iterator() {
return new Itr();
}
-----------------------------------------------------------------------------------------------------------------------
package cai.util;
import java.util.*;
public class MyArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, java.io.Serializable{
/**
* 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;
//public int modCount; modCount is defined in AbstractList
public MyArrayList(int initialCapacity){
super();
if(initialCapacity < 0){
throw new IllegalArgumentException("Illegal Capacity :" + initialCapacity);
}
elementData = (E[]) new Object[initialCapacity];
}
public MyArrayList(){
this(10);
}
public void trimToSize(){
modCount ++;
int oldCapacity = elementData.length;
if(size < oldCapacity){
elementData = Arrays.copyOf(elementData, size);
}
}
public void ensureCapacity(int minCapacity){
modCount++;
int oldCapacity = elementData.length;
if(minCapacity > oldCapacity){
elementData = Arrays.copyOf(elementData, minCapacity);
}
}
/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
public int size(){
return size;
}
/**
* Returns <tt>true</tt> if this list contains no elements.
*
* @return <tt>true</tt> if this list contains no elements
*/
public boolean isEmpty(){
return size == 0;
}
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
public E set(int index, E element) {
RangeCheck(index);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
private void RangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i<size; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
Object[] a = elementData = new Object[arrayLength];
for (int i=0; i<size; i++)
a[i] = s.readObject();
}
}
- 链表之ArrayList
- 数据结构之链表(附:数组与链表对比、ArrayList/LinkedList源码分析)
- java 集合 之 链表和线性表以及ArrayList的各方法的介绍及示例 及 ArrayList与LinkedList的区别分析 及 ArrayList与Array(数组)的区别
- Java学习之ArrayList
- Java学习之ArrayList
- Java集合之ArrayList
- 集合之ArrayList
- 类实现之ArrayList
- 源码分析之ArrayList
- java集合之ArrayList
- jdk1.6之ArrayList
- Java集合之ArrayList
- java 之ArrayList实现
- java集合之ArrayList
- C# 之 集合ArrayList
- java集合之arrayList
- 实验田之打印ArrayList
- java之封装ArrayList
- 学习要有目标
- Framebuffer Driver
- c# sql 2005express
- accept函数.
- java中的排序算法
- 链表之ArrayList
- 好久没看到UML的论战了
- 关于IE浏览器装flash控件就假死的解决办法
- 写需求分析感想
- 在弹出对话框中的Edit Control中指定光标
- 5644
- Cloud Zooml插件实现图片列表放在效果
- 关于MFC的一些理解
- 经典代码下载