数据结构和算法------ArrayList的实现
2016-11-13 16:33
183 查看
概述:今年的秋招接近尾声了,在秋招的笔试和面试中,反映出了我自己技术栈的很多不足,最致命的地方在于数据结构和算法,计算机网络.在拿了三个Offer之后,结束秋招之旅.现在沉下心来,重新开始学习数据结构和算法,并且记录每一种数据结构的Java语言实现.同时也开始拜读[ThingKing in Java]和[Effective Java]这两本著作.争取在2017年的春招中拿到一份更加满意的Offer.
ArrayList的实现
public class MyArrayList<AnyType> implements Iterable<AnyType>{ private static final int DEFAULT_CAPACITY=10; private int theSize;//ArrayList的长度 private AnyType [] theItems;//内部使用数组实现 public MyArrayList(){ //构造方法 doClear(); } public void clear(){ doClear(); } private void doClear(){ theSize=0;//大小清零 ensureCapacity(DEFAULT_CAPACITY);//容量改为默认大小 } /* 返回ArrayList大小 */ public int size(){ return theSize; } /* 判空 */ public boolean isEmpty(){ return size()==0; } /* 将容量大小设置为数组大小 */ public void trimToSize(){ ensureCapacity(size()); } /* 查询 */ public AnyType get(int index){ if (index<0||index>=size()) { throw new ArrayIndexOutOfBoundsException(); } return theItems[index]; } /* 修改 @return old 被修改位置处的原值 */ public AnyType set(int index,AnyType newVal){ if (index<0||index>=size()) { //容错处理 throw new ArrayIndexOutOfBoundsException(); } AnyType old=theItems[index]; theItems[index]=newVal; return old; } /* 重置容量大小 */ @SuppressWarnings("unchecked") public void ensureCapacity(int newCapacity){ if (newCapacity<theSize) { return; } AnyType [] old=theItems; //存储对原始数组的一个引用 theItems=(AnyType[])new Object [newCapacity];//为新数组分配内存 for (int i=0; i<size(); i++) { theItems[i]=old[i]; //将旧的内容拷贝到新的数组中去 } } /* 添加元素,默认添加到组数的尾部 */ public boolean add (AnyType x){ add(size(),x); return true; } /* 添加元素到 index 位置处 */ public void add (int index,AnyType x){ if (theItems.length==size()) {//当内部数组长度等于列表长时,就需要扩充容量 ensureCapacity(size()*2+1); //此处+1是用于初始容量是0的情形 } for (int i=theSize;i>index ;i--) { theItems[i]=theItems[i-1];//从后往前,将元素一个一个的后后移 } theItems[index]=x; theSize++; //将列表的长度+1 } /* 删除 */ public AnyType remove(int index){ AnyType removeItem=theItems[index]; for (int i=index; i<size()-1;i++ ) { theItems[i]=theItems[i+1]; } theSize--; //将列表的长度-1 return removeItem; //返回被删除的元素值 } /* 实现Iterable接口的集合都必须提供一个名字为Iterator的方法,返回一个Iterator类型的对象 */ @Override public java.util.Iterator<AnyType> iterator(){ return new ArrayListIterator(); } /* */ private class ArrayListIterator implements java.util.Iterator<AnyType>{ private int current=0; public boolean hasNext(){ return current<size(); } public AnyType next(){ if (!hasNext()) { throw new java.util.NoSuchElementException(); } return theItems[current++]; } public void remove(){ //由于迭代器的remove()可能与MyArrayList的remove()冲突,因此我们必须使用MyArrayList.this.remove MyArrayList.this.remove(--current); } } }
相关文章推荐
- Android版数据结构与算法(二):基于数组的实现ArrayList源码彻底分析
- 在网上看到使用PHP实现常见的数据结构和算法
- 数据结构所有算法C#、VB实现
- 数据结构—中缀表达式转后缀表达式算法及实现—栈的应用—计算表达式(C++代码实现)(1)
- 数据结构与算法之—顺序栈c++面向对象实现
- 数据结构与算法——三种基础排序算法C#实现(冒泡排序、选择排序、插入排序)
- 在网上看到使用PHP实现常见的数据结构和算法
- 算法与数据结构--实现线性表的删除操作--算法2.4
- 算法与数据结构课程中的c++实现的顺序表和链表
- C#数据结构和算法学习系列七----队列、队列的实现和应用
- 数据结构与算法之—两个栈实现一个队列
- 【数据结构】有向图、无向图以及最短路(Dijkstra)算法的C#实现(Template模式)
- 我的C算法库[5]:实现数据结构ArrayList
- C#数据结构和算法 [Arrays and ArrayLists]
- 数据结构各种算法实现C++
- 数据结构复习:几种排序算法的C++实现和二叉树的相关算法实现
- Java中如何实现Tree的数据结构算法
- 数据结构经典算法汇总___图的邻接矩阵实现
- 在网上看到使用PHP实现常见的数据结构和算法
- 算法与数据结构--实现线性表的插入操作--算法2.3