自定义ArrayList
package list;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyArrayList<T> implements Iterable<T>{
private static final int DEFAULT_CAPACITY=10 ;
private int size;
private T[] items;
public MyArrayList (){
doClear();
}
public void clear (){
doClear();
}
public void doClear (){
size=0 ;
ensuereCapacity(DEFAULT_CAPACITY);
}
public int size (){
return size;
}
public boolean isEmpty (){
return size()==0 ;
}
public void trimToSize (){
ensuereCapacity(size());
}
public T get (int index){
if (index<0 ||index>size()) {
throw new ArrayIndexOutOfBoundsException();
}
else {
return items[index];
}
}
public T set (int index,T value ){
rangeCheck(index);
T old=items[index];
items[index]=value ;
return old;
}
public void ensuereCapacity (int newCapacity){
if (newCapacity<size) {
return ;
}
T[] old=items;
items=(T[]) new Object[DEFAULT_CAPACITY];
for (int i = 0 ; i < size(); i++) {
items[i]=old[i];
}
}
public boolean add (T value ){
add(size(), value );
return true ;
}
public void add (int index, T value ){
rangeCheck(index);
if (items.length==size()) {
ensuereCapacity(size()*2 +1 );
}
for (int i = size; i > index; i--) {
items[i]=items[i-1 ];
}
items[index]=value ;
size++;
}
public T remove (int index){
rangeCheck(index);
T removeValue=items[index];
for (int i = index; i < size-1 ; i++) {
items[i]=items[i+1 ];
}
items[size--]=null ;
return removeValue;
}
public void rangeCheck (int index){
if (index>size||index<0 ) {
throw new ArrayIndexOutOfBoundsException();
}
}
@Override
public Iterator<T> iterator () {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator<T>{
private int current=0 ;
@Override
public boolean hasNext () {
return current<size();
}
@Override
public T next () {
if (!hasNext()) {
throw new NoSuchElementException();
}
return (T) items[current++];
}
@Override
public void remove () {
MyArrayList.this .remove(--current);
}
}
}
自定义LinkedList
package list;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyLinkedList<T> implements Iterable<T>{
private Node<T> beginMarker;
private Node<T> endMarker;
private int size;
private int modCount=0 ;
public MyLinkedList () {
doClear();
}
private static class Node<T>{
public T data;
public Node<T> prev;
public Node<T> next;
public Node (T data,Node<T> prev,Node<T> next){
this .data=data;
this .prev=prev;
this .next=next;
}
}
public void clear (){
doClear();
}
public void doClear (){
beginMarker=new Node<T>(null , null , null );
endMarker=new Node<T>(null , beginMarker, null );
beginMarker.next=endMarker;
size=0 ;
modCount++;
}
public int size (){
return size;
}
public boolean isEmpty (){
return size()==0 ;
}
public boolean add (T value ){
add(size(), value );
return true ;
}
public void add (int index,T value ){
addBefore(getNode(index, 0 , size()), value );
}
public T remove (int index){
return remove(getNode(index));
}
public T get (int index){
return getNode(index).data;
}
public T set (int index,T value ){
T old=getNode(index).data;
getNode(index).data=value ;
return old;
}
private void addBefore (Node<T> p,T value ){
Node<T> newNode=new Node<T>(value , p.prev, p);
newNode.prev.next=newNode;
size++;
modCount++;
}
private T remove (Node<T> p){
p.prev.next=p.next;
p.next.prev=p.prev;
size--;
modCount++;
return p.data;
}
private Node<T> getNode (int index){
return getNode(index, 0 , size()-1 );
}
private Node<T> getNode (int index,int lower,int upper){
Node<T> p;
if (index<lower||index>upper) {
throw new IndexOutOfBoundsException();
}
if (index<size()/2 ) {
p=beginMarker.next;
for (int i = 0 ; i < index; i++) {
p=p.next;
}
}
else {
p=endMarker;
for (int i = size(); i >index; i--) {
p=p.prev;
}
}
return p;
}
public void rangeCheck (int index){
if (index>size()) {
throw new ArrayIndexOutOfBoundsException();
}
}
@Override
public Iterator<T> iterator () {
return new MyLinkedListIterator();
}
private class MyLinkedListIterator implements Iterator<T>{
private Node<T> current=beginMarker.next;
private int expectedModCount=modCount;
private boolean okToRemove=false ;
@Override
public boolean hasNext () {
return current.next!=endMarker;
}
@Override
public T next () {
if (modCount!=expectedModCount) {
throw new ConcurrentModificationException();
}
if (!hasNext()) {
throw new NoSuchElementException();
}
T nextItem=current.data;
current=current.next;
okToRemove=true ;
return nextItem;
}
@Override
public void remove () {
if (modCount!=expectedModCount) {
throw new ConcurrentModificationException();
}
if (!okToRemove) {
throw new IllegalStateException();
}
MyLinkedList.this .remove(current.prev);
okToRemove=false ;
}
}
}