java數據結構之(一):ArrayList類的實現

  ArrayList是鏈表(List)的一種實現,通過數組的方式實現的,所具有的特點就是數組本身的特點,比如取數據較快,刪除和添加元素比較耗時等等,後續日誌需要詳細介紹ArrayList的特點及原理。先貼上ArrayList的實現代碼(數據結構與算法分析_java版)。

package com.biyao.datastructure.list;

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

/**
 * @ClassName: MyArrayList
 * @Description: ArrayList實現
 * @author yangy
 * @date 2017年7月28日 下午3:01:47
 */
public class MyArrayList<AnyType>{

	//默認容量
	private static final int DEFAULT_CAPACITY = 10;
	//list的長度
	private int theSize;
	//元素數組
	private AnyType [] theItems;
	/**
	 * <p>Title: 構造方法</p>
	 * <p>Description: 需清空鏈表</p>
	 */
	public MyArrayList(){
		clear();
	}
	
	/**
	 * @Title: clear
	 * @Description: 清空鏈表
	 * @return void
	 */
	public void clear(){
		theSize = 0;
		ensureCapacity(DEFAULT_CAPACITY);
	}
	/**
	 * @Description: 獲取鏈表大小
	 * @return int 鏈表大小
	 */
	public int size(){
		return theSize;
	}
	
	/**
	 * @Description: 判斷鏈表是否爲空
	 * @return 
	 */
	public boolean isEmpty(){
		return size() == 0;
	}
	
	public void trimToSize(){
		ensureCapacity(size());
	}
	
	/**
	 * @Description: 根據索引獲取鏈表元素
	 * @param idx 索引值
	 * @return 鏈表中索引值對應的元素值
	 */
	public AnyType get(int idx){
		if(idx < 0 || idx >= size()){
			throw new ArrayIndexOutOfBoundsException();
		}
		return theItems[idx];
	}
	
	/**
	 * @Description: 向鏈表中指定索引處添加元素
	 * @param idx 索引值
	 * @param newVal 添加的元素
	 * @return 鏈表set前索引處的舊元素
	 */
	public AnyType set(int idx,AnyType newVal){
		if(idx < 0 || idx >= size()){
			throw new ArrayIndexOutOfBoundsException();
		}
		AnyType old = theItems[idx];
		theItems[idx] = newVal;
		return old;
	}
	
	/**
	 * @Description: 鏈表擴容
	 * @param newCapacity 鏈表新容量大小
	 * @return void
	 */
	@SuppressWarnings("unchecked")
	public void ensureCapacity(int newCapacity){
		if(newCapacity <= theSize){
			return ;
		}
		AnyType [] old = theItems;
		theItems = (AnyType[])new Object[newCapacity];
		for (int i = 0; i < theSize; i++) {
			theItems[i] = old[i];
		}
		
	}
	
	/**
	 * @Description: 向鏈表中添加元素
	 * @param x 新元素
	 * @return 添加是否成功
	 */
	public boolean add(AnyType x){
		add(size(),x);
		return true;
	}
	
	/**
	 * @Description: 向鏈表固定索引位置添加元素
	 * @param idx 索引位置
	 * @param x 待添加元素
	 */
	public void add(int idx,AnyType x){
		if(theItems.length == size()){
			ensureCapacity(size()*2 + 1);
		}
		for (int i = theSize; i > idx; i--) {
			theItems[i] = theItems[i-1];
		}
		theItems[idx] = x;
		theSize++;
	}
	
	/**
	 * @Description: 刪除鏈表指定索引位置的元素
	 * @param idx 索引位置
	 * @return 被刪元素
	 */
	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;
	}
	
	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++];
		}
		
		public void remove(){
			MyArrayList.this.remove(--current);
		}
	}
	
}


測試代碼如下:

package com.biyao.datastructure.list;

import java.util.Iterator;

/**
 * @ClassName: MyArrayListExample
 * @Description: MyArrayList 測試類
 * @author yangy
 * @date 2017年7月28日 下午7:21:49
 */
public class MyArrayListExample {
	
	public static void main(String[] args) {
		MyArrayList<String> arrayList = new MyArrayList<String>();
		System.out.println("arrayList是否爲空1:" + arrayList.isEmpty());
		arrayList.add("a");
		arrayList.add("b");
		arrayList.add("c");
		System.out.println("arrayList是否爲空2:" + arrayList.isEmpty());
		System.out.println("arrayList大小爲:" + arrayList.size());
		System.out.println("arrayList.get(1)值爲:" + arrayList.get(1));
		System.out.println("arrayList.remove(1)返回值爲:" + arrayList.remove(1));
		System.out.println("arrayList.get(1)值爲:" + arrayList.get(1));
		arrayList.set(1, "d");
		System.out.println("arrayList.get(1)值爲:" + arrayList.get(1));
		
		//遍歷list
		Iterator<String> itor = arrayList.iterator();
		System.out.println("遍歷list:");
		while(itor.hasNext()){
			String s = itor.next();
			if(s.equals("d")){
				itor.remove();
			}
			System.out.print(s + " ");
		}
		System.out.println();
		System.out.println("arrayList大小爲:" + arrayList.size());
	}

}




發佈了66 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章