JAVA数据结构--ArrayList动态数组

在计算机科学中,动态数组,可扩展数组可调整数组动态表可变数组数组列表是一种随机存取可变大小列表数据结构,允许添加或删除元素。它提供许多现代主流编程语言的标准库。动态数组克服了静态数组的限制,静态数组具有需要在分配时指定的固定容量。

动态数组与动态分配的数组不同,数组是数组分配时大小固定的数组,尽管动态数组可能使用固定大小的数组作为后端。

  

  代码实现:

 1 package DataStructures;
 2 
 3 import java.util.Iterator;
 4 import java.util.NoSuchElementException;
 5 
 6 public class MyArrayList<AnyType> implements Iterable<AnyType> {
 7 
 8     private static final int DEFAULT_CAPACITY=10;
 9     private int theSize;
10     private AnyType[] theItems;
11     public MyArrayList() {
12         doClear();
13         // TODO Auto-generated constructor stub
14     }
15     private void clear(){
16         doClear();
17     }
18     private void doClear(){    //移除动态数组中所有元素
19         theSize=0;
20         ensureCapacity(DEFAULT_CAPACITY);
21     }
22     public int size(){    //返回当前动态数组大小
23         return theSize;
24     }
25     public boolean isEmpty(){
26         return size()==0;
27     }
28     public void trimToSize(){    //将动态数组的容量调整为当前实例的大小
29         ensureCapacity(size());
30     }
31     public AnyType get(int idx){
32         if(idx<0||idx>=size())
33             throw new ArrayIndexOutOfBoundsException();
34         return theItems[idx];
35     }
36     public AnyType set(int idx,AnyType newVal){
37         if(idx<0||idx>=size())
38             throw new ArrayIndexOutOfBoundsException();
39         AnyType old =theItems[idx];
40         theItems[idx]=newVal;
41         return old;
42     }
43     private void ensureCapacity(int newCapacity) {    //为当前的动态数组扩容
44         if(newCapacity<theSize)
45             return;
46         AnyType[] old=theItems;
47         theItems=(AnyType []) new Object[newCapacity];
48         for(int i=0;i<size();i++)
49             theItems[i]=old[i];
50     }
51     public boolean add(AnyType x){
52         add(size(),x);
53         return true;
54     }
55     public void add(int idx,AnyType x){    //添加数据
56         if(theItems.length==size())
57             ensureCapacity(size()*2+1);    //进行扩容
58         for(int i=theSize;i>idx;i--){
59             theItems[i]=theItems[i-1];
60         }
61         theItems[idx]=x;
62         theSize++;
63     }
64     public AnyType remove(int idx){
65         AnyType removeItem=theItems[idx];
66         for(int i=idx;i<size()-1;i++)
67             theItems[i]=theItems[i+1];
68         theSize--;
69         return removeItem;
70     }
71     @Override
72     public Iterator<AnyType> iterator() {    //迭代器iterator方法返回的是ArrayListIterator的一个实例
73         // TODO Auto-generated method stub
74         return new ArrayListIterator();
75     }
76     private class ArrayListIterator implements Iterator<AnyType>{
77         private int current=0;
78         public boolean hasNext(){
79             return current<size();
80         }
81         public AnyType next(){
82             if(!hasNext())
83                 throw new NoSuchElementException();
84             return theItems[current++];
85         }
86         public void remove(){
87             MyArrayList.this.remove(--current);
88         }
89     }
90 }
原文地址:https://www.cnblogs.com/vi3nty/p/7503985.html