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());
	}

}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值