自己写的一个ArrayList
2009-09-22 16:18
246 查看
@SuppressWarnings("unchecked") public class ArrayList<T> { private T[] listArr; private int listSize; //构造方法 public ArrayList(){ listArr = (T[]) new Object[10]; listSize = 0; } //设置更大的容量 public void ensureCapacity(int minCapacity){ // get the current capacity int currentCapacity = listArr.length; // only take action if the requested capacity is larger than the existing capacity if(minCapacity > currentCapacity){ T[] t = (T[]) new Object[minCapacity]; for(int i=0;i<=listSize-1;i++){ t[i]=listArr[i]; } listArr = t; } } //把列表容量调整为它的当前大小 public void trimToSize(){ // get the current capacity int currentCapacity = listArr.length; // only take action if the current capacity is larger than listSize if(currentCapacity > listSize){ T[] t = (T[]) new Object[listSize]; for(int i=0;i<=listSize-1;i++){ t[i]=listArr[i]; } listArr = t; } } //验证index是否位于正确的范围内 private void rangecheck(int index,String msg,int upperBound){ if(index < 0 || index > upperBound){ throw new IndexOutOfBoundsException( "/n"+msg+": index "+index+ " out of bounds.Should be in the range 0 to "+ upperBound); } } //在索引index处添加元素 public void add(int index, T element) { // index==listSize is valid; append to the list this.rangecheck(index, "ArrayList add()", listSize); // see if we need to reallocate more memory if(listSize==listArr.length){ this.ensureCapacity(2*listArr.length); } // shift elements at positions index through listSize-1 to the right for(int i=listSize-1;i>=index;i--){ listArr[i+1] = listArr[i]; } // insert element at location index and increment the list size listArr[index] = element; listSize++; } //在尾部添加元素并返回true public boolean add(T element) { this.add(listSize, element); return true; } //返回第一个element元素的索引 public int indexOf(T element) { for(int i=0;i<=listSize-1;i++){ if(listArr[i].equals(element)) return i; } return -1; } //返回最后一个element元素的索引 public int lastIndexOf(T element) { for(int i=listSize-1;i>=0;i--){ if(listArr[i].equals(element)) return i; } return -1; } //删除索引index处的元素 public T remove(int index) { // verify that index is in the proper range this.rangecheck(index, "ArrayList remove()", listSize-1); // save the return value T returnElement = listArr[index]; // shift elements at indices index+1 to listSize-1 left one position for(int i=index;i<listSize-1;i++){ listArr[i] = listArr[i+1]; } // make former last entry a null reference and decrement list size listArr[listSize-1] = null; listSize--; // return the value that was removed return returnElement; } //删除列表中的element元素 public boolean remove(T element) { int index = this.indexOf(element); if(index != -1){ this.remove(index); return true; } return false; } //得到索引index处的元素 public T get(int index) { // verify that index is in the proper range this.rangecheck(index, "ArrayList get()", listSize-1); return listArr[index]; } //为索引index处的元素赋值 public T set(int index, T element) { // verify that index is in the proper range this.rangecheck(index, "ArrayList set()", listSize-1); // save the element at listArr[index] T previousValue = listArr[index]; // assign the element at index index listArr[index] = element; // return the previous element return previousValue; } //返回列表的长度 public int size() { return listSize; } //判断列表是否为空 public boolean isEmpty() { return listSize==0; } //清空列表 public void clear() { for (int i = 0; i < listSize; i++) { listArr[i] = null; } listSize = 0; } //判断列表中是否存在元素element public boolean contains(T element) { return !(this.indexOf(element)==-1); } //返回对应的数组 public T[] toArray() { T[] t = (T[]) new Object[listSize]; for (int i = 0; i < listSize; i++) { t[i] = listArr[i]; } return t; } }
相关文章推荐
- 自己实现一个ArrayList,模仿ArrayList类的底层结构
- 自己动手系列——实现一个简单的ArrayList
- 自己实现一个简单的ArrayList
- 小伙伴来自己实现一个ArrayList
- 根据数组的原理,自己实现一个简易版的ArrayList
- java中自己实现一个ArrayList
- 自己写的一个ArrayList
- 自己写一个简单的ArrayList
- 自己实现一个简单ArrayList
- java集合之ArrayList源码解读 帮助大家自己动手写一个ArrayList
- [JAVA]集合框架之自己实现一个具有基本增查删改功能的ArrayList
- 自己打造一个ArrayList
- 如何自己写一个ArrayList、LinkedList和HashSet集合?
- 自己实现一个泛型ArrayList
- 自己写的一个ArrayLIST,but 不知道怎么很好的实现System底下的arraycopy方法
- 自己写的一个Java Bean转换为HashMap及ArrayList容器的功能类,支持Hibernate
- 用java做一个自己的基于XMPP协议IM(即时通讯)
- 分享一个自己写的table表格排序js插件(高效简洁)
- 用ASP.NET做一个自己的新闻发布RSS
- 自己写一个IOC注解框架,里面有分析xutils3跟utterknife的IOC的源码分析--binbinyang