实现自己的ArrayList

导读:本篇文章讲解 实现自己的ArrayList,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

最近在学习数据结构和算法,书上有个ArrayList的简单实现,写的很不错。

  1 package cn.sp.test4;
  2 
  3 import java.util.Iterator;
  4 import java.util.NoSuchElementException;
  5 
  6 /**
  7  * Created by 2YSP on 2017/10/9.
  8  */
  9 public class MyArrayList<AnyType> implements Iterable<AnyType> {
 10 
 11     private static  final  int DEFAULT_CAPACITY = 10;
 12 
 13     private int theSize;
 14     private AnyType[] theItems;
 15 
 16     public MyArrayList(){
 17         doClear();
 18     }
 19 
 20     private void doClear() {
 21         theSize = 0;
 22         ensureCapacity(DEFAULT_CAPACITY);
 23     }
 24 
 25     public int size(){
 26         return theSize;
 27     }
 28 
 29     public boolean isEmpty(){
 30         return size() == 0;
 31     }
 32 
 33     public void trimToSize(){
 34         ensureCapacity(size());
 35     }
 36 
 37     /**
 38      * 获取角标处的值
 39      * @param idx
 40      * @return
 41      */
 42     public AnyType get(int idx){
 43         if (idx < 0 || idx >= size()){
 44             //角标越界异常
 45             throw new ArrayIndexOutOfBoundsException();
 46         }
 47 
 48         return theItems[idx];
 49     }
 50 
 51     /**
 52      * 替换目标位置的值,并返回旧值
 53      * @param idx
 54      * @param newVal
 55      * @return
 56      */
 57     public AnyType set(int idx , AnyType newVal){
 58         if (idx < 0 || idx >= size()){
 59             //角标越界异常
 60             throw new ArrayIndexOutOfBoundsException();
 61         }
 62 
 63         AnyType old = theItems[idx];
 64         theItems[idx] = newVal;
 65         return old;
 66     }
 67 
 68     public void ensureCapacity(int newCapacity) {
 69         if (newCapacity < theSize){
 70             return;
 71         }
 72 
 73         AnyType[] old = theItems;
 74         theItems = (AnyType[]) new Object[newCapacity];
 75         for(int i = 0 ;i < old.length ; i++){
 76             theItems[i] = old[i];
 77         }
 78     }
 79 
 80     public boolean add(AnyType x){
 81         add(size(),x);
 82         return true;
 83     }
 84 
 85     public void add(int idx,AnyType x){
 86         if (theItems.length == size()){
 87             //如果已满则扩容为之前的两倍
 88             ensureCapacity(size()*2 + 1);
 89         }
 90 
 91         for (int i = theSize ; i > idx ; i--){
 92             theItems[i] = theItems[i-1];//依次后挪
 93         }
 94 
 95         theItems[idx] = x;
 96         theSize++;
 97     }
 98 
 99     public AnyType remove(int idx){
100         AnyType removedItem = theItems[idx];
101         for(int i = idx ; i <size()-1;i++){//依次前移
102             theItems[i] = theItems[i+1];
103         }
104         theSize--;
105         return removedItem;
106     }
107 
108     @Override
109     public Iterator<AnyType> iterator() {
110         return new ArrayListIterator();
111     }
112 
113     private class ArrayListIterator<AnyType> implements java.util.Iterator<AnyType>{
114 
115         private int current = 0;
116 
117         @Override
118         public boolean hasNext() {
119             return current < size();
120         }
121 
122         @Override
123         public AnyType next() {
124             if (!hasNext()){
125                 throw new NoSuchElementException();
126             }
127 
128             return (AnyType) theItems[ current++ ];
129         }
130 
131         @Override
132         public void remove() {
133             MyArrayList.this.remove(current--);
134         }
135     }
136 }

注意理解current++和current–

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/13281.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!