Java容器实现——ArrayList和LinkedList

public class MyArrayList<AnyType> implements Iterable<AnyType> {
	private static final int DEFAULT_CAPACITY = 10;
	
	private int theSize;
	@SuppressWarnings("unchecked")
	private AnyType[] theItems = (AnyType[])new Object[DEFAULT_CAPACITY];
	
	public 	MyArrayList()
	{
		System.out.println("MyArraryList!");
		clear();
	}
	
	private void clear() {
		theSize=0;
		ensureCapacity(DEFAULT_CAPACITY);
	}




	public int size()
	{
		return theSize;
	}


	public boolean isEmpty()
	{
		return size()==0;
	}
	
	public void trimToSize()
	{
		ensureCapacity(size());
	}
	
	public AnyType get(int idx)
	{
		if(idx<0||idx>=size())
		{
			throw new ArrayIndexOutOfBoundsException();
		}
		return theItems[idx];
	}
	
	public AnyType set(int idx,AnyType newValue){
		if(idx<0 || idx>=size()){
			throw new ArrayIndexOutOfBoundsException();
		}
		AnyType old = theItems[idx];
		theItems[idx] = newValue;
		return old;
	}
	
	@SuppressWarnings("unchecked")
	public void ensureCapacity(int newCapacity) {
		if(newCapacity < theSize)
		{
			return ;
		}
		AnyType[] old = theItems;
		theItems = (AnyType[])new Object[newCapacity];
		System.out.println(old.length);
		for(int i = 0 ; i < size() ;i++){
			theItems[i] = old[i];
		}
	}
	
	public boolean add(AnyType x)
	{
		add(size(),x);
		return true;
		
	}
	
	public void add(int idx,AnyType x){
		if(theItems.length == size()) {
			ensureCapacity(size()*2+1);
		}
		System.out.println(theItems.length);
		for(int i=theSize ;i>idx;i--){
			
			theItems[i]=theItems[i-1];
		}
		theItems[idx]=x;
		theSize++;
	}
	
	public AnyType remove(int idx) {
		AnyType removeItem = theItems[idx];
		for(int i = idx;i < size()-1;i++){
			theItems[i]=theItems[i+1];
		}
		theSize--;
		return removeItem;
	}
	
	@Override
	public Iterator<AnyType> iterator() {
		return new ArrayListIterator();
	}
	
	private class ArrayListIterator implements Iterator<AnyType> {


		private int current = 0;
		@Override
		public boolean hasNext() {
			return current < size();
		}


		@Override
		public AnyType next() {
			if(!hasNext())throw new NoSuchElementException();
			return theItems[current++];
		}


		@Override
		public void remove() {
			MyArrayList.this.remove(--current);
		}
		
	}
	
	public static void main(String[] args) {
		MyArrayList<String> list = new MyArrayList<String>();
		for(int i = 0; i<15;i++) {
			list.add("Hello Wrod!"+i);
			System.out.println("size:"+list.size());
		}
		
		for(int i = 0; i<list.size();i++) {
			System.out.println(list.get(i));
		}
		
		Iterator<String> iterator = list.iterator();
		while(iterator.hasNext()){
			System.out.println(iterator.next());
			iterator.remove();
		}
		while(iterator.hasNext()){
			System.out.println(iterator.next());
		}
		for(int i = 0; i<list.size();i++) {
			System.out.println(list.get(i));
		}
		System.out.println("size:"+list.size());
	}
	


}


LinkedList模拟实现:

package chapter3.ListStackQueue;


import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;




public class MyLinkedList<AnyType> implements Iterable<AnyType>{
	
	private int theSize;
	private int modCount=0;
	private Node<AnyType> beginMaker;
	private Node<AnyType> endMaker;
	
	private static class Node<AnyType> {
		public AnyType data;
		public Node<AnyType> prev;
		public Node<AnyType> next;
		public Node(AnyType d,Node<AnyType> p,Node<AnyType> n) {
			data = d;
			prev=p;
			next=n;
		}
	}
	
	public MyLinkedList() {
		clear();
	}
	
	public void clear() {
		beginMaker = new Node<AnyType>(null, null, null);
		endMaker = new Node<AnyType>(null, beginMaker, null);
		beginMaker.next = endMaker;
		theSize=0;
		modCount++;
	}
	
	public int size() {
		return theSize;
	}
	
	public boolean isEmpty() {
		return size()==0;
	}
	
	
	public boolean add(AnyType x) {
		add(size(),x);
		return true;
	}
	
	public void add(int idx ,AnyType x) {
		addBefore(getNode(idx),x);
	}
	
	public AnyType set(int idx,AnyType newVal) {
		Node<AnyType> p = getNode(idx);
		AnyType oldVal = p.data;
		p.data = newVal ;
		return oldVal;
	}
	
	public AnyType remove(int idx) {
		return remove(getNode(idx));
	}
	
	private void addBefore(Node<AnyType> p,AnyType x) {
		Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p );
		newNode.prev.next = newNode;
		p.prev = newNode;
		theSize++;
		modCount++;
	}
	
	private AnyType remove(Node<AnyType> p) {
		p.next.prev = p.prev;
		p.prev.next = p.next;
		theSize--;
		modCount++;
		return p.data;
	}
	
	private Node<AnyType> getNode(int idx) {
		Node<AnyType> p;
		if(idx < 0 || idx > size()) {
			throw new IndexOutOfBoundsException();
		}
		
		if(idx < size()/2) {
			p = beginMaker.next;
			for(int i = 0 ; i< idx; i++) {
				p = p.next;
			}
		}else {
			p = endMaker;
			for(int i = size();i > idx ; i--) {
				p = p.prev;
			}
		}
		return p ;
	}
	
	@Override
	public Iterator<AnyType> iterator() {
		return new LinkedListIterator();
	}
	
	private class LinkedListIterator implements Iterator<AnyType>{
		private Node<AnyType> current = beginMaker.next;
		private int expectedModCount = modCount;
		private boolean okToRemove = false;
		
		public boolean hasNext() {
			return current!=endMaker;
		}
		public AnyType next() {
			if(modCount != expectedModCount) throw new ConcurrentModificationException();
			if(!hasNext())throw new NoSuchElementException();
			AnyType nextItem = current.data;
			current = current.next;
			okToRemove = true;
			return  nextItem;
		}
		
		public void remove( ){
			if(modCount != expectedModCount) throw new ConcurrentModificationException();
			if(!okToRemove) throw new IllegalStateException();
			
			MyLinkedList.this.remove(current.prev);
			okToRemove = false;
			expectedModCount++;
		}
	}


	public static void main(String[] args) {
		MyLinkedList<String> linkedList = new MyLinkedList<String>();
		for(int i = 0;i<15;i++) {
			linkedList.add("Hello word!"+i);
		}
		for(int i = 0;i<linkedList.size();i++) {
			System.out.println(linkedList.getNode(i).data);
		}
		System.out.println("\nIterator method:");
		Iterator iterator = linkedList.iterator();
		int i = 0;
		while(iterator.hasNext()) {
			i++;
			System.out.println(iterator.next());
			if(i%2==0)iterator.remove();
		}
		
		Iterator iterator2 = linkedList.iterator();
		while(iterator2.hasNext()) {
			System.out.println(iterator2.next());
		}
		
	}


}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值