01 不可小瞧数组

1. 数组数据结构的封装

  1 /**
  2  * @author 阿遠
  3  * Date: 2019/1/13
  4  * Time: 19:29
  5  */
  6 // 数组操作接口的实现
  7 public class Array<E> {
  8 
  9     private E[] data;
 10     private  int size; // 数组中实际元素数量
 11 
 12     // 构造函数,传入数组的容量capacity构造Array
 13     public Array(int capacity) {
 14         data = (E[]) new Object[capacity];
 15         size = 0;
 16     }
 17     // 默认构造函数,默认数组的容量capacity=10
 18     public Array() {
 19         this(10);
 20     }
 21     // 获取数组中的元素个数
 22     public int getCapacity() {
 23         return size;
 24     }
 25     // 判断数组是否为空
 26     public boolean isEmpty() {
 27         return size == 0;
 28     }
 29     // 向所有元素后添加一个新元素
 30     public void addLast(E e) {
 31 //        if (size == data.length){
 32 //            throw new IllegalArgumentException("addLast failed!Array is full");
 33 //        }
 34 //        data[size] = e;
 35 //        size++;
 36         // 复用add
 37         add(size, e);
 38     }
 39     //在所有元素前添加新元素
 40     public void addFirst(E e) {
 41         add(0, e);
 42     }
 43     // 在index个位置插入一个新元素,扩容成动态数组,此处我们扩容成原来的2倍,在ArrayList中为1.5倍
 44     public void add(int index, E e) {
 45 
 46         if (index < 0 || index > size) {
 47             throw new IllegalArgumentException("Add failed!Index required >=0 and <size");
 48         }
 49         if (size == data.length){
 50             resize(2 * data.length);
 51         }
 52         for (int i = size-1; i >= index; i--){
 53             data[i+1] = data[i];
 54         }
 55         data[index] = e;
 56         size ++;
 57     }
 58     // 给数组扩容或者缩容
 59     private void resize(int newCapacity) {
 60         E[] newData = (E[]) new  Object[newCapacity];
 61         for (int i = 0; i < size; i++) {
 62             newData[i] = data[i];
 63         }
 64         data = newData;
 65     }
 66     // 获取index索引位置的元素
 67     public E get(int index) {
 68         if (index < 0 || index > size) {
 69             throw new IllegalArgumentException("Get failed!Index is illegal.");
 70         }
 71         return data[index];
 72     }
 73     //修改index元素索引位置的元素为e
 74     public void  set(int index, E e) {
 75         if (index < 0 || index > size) {
 76             throw new IllegalArgumentException("Set failed!Index is illegal.");
 77         }
 78         data[index] = e;
 79     }
 80 
 81     @Override
 82     public  String toString() {
 83         StringBuilder res = new StringBuilder();
 84         res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
 85         res.append("[");
 86         for (int i = 0; i < size; i++) {
 87             res.append(data[i]);
 88             if (i != size-1)
 89                 res.append(", ");
 90         }
 91         res.append("]");
 92         return res.toString();
 93     }
 94 
 95     // 查找数组中是否存在元素e
 96     public boolean contains(E e) {
 97         for (int i= 0; i < size; i++) {
 98             if (data[i] == e)
 99                 return true;
100         }
101         return false;
102     }
103     // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
104     public int find(E e) {
105         for (int i = 0; i < size; i++) {
106             if (data[i] == e)
107                 return  i;
108         }
109         return -1;
110     }
111     // 从数组中删除元素,返回删除的元素
112     public E remove(int index) {
113         if (index < 0 || index >= size) {
114             throw new IllegalArgumentException("Remove failed!Index is illegal.");
115         }
116         E ret = data[index];
117         for (int i = index + 1; i < size; i++) {
118             data[i-1] = data[i];
119         }
120         size --;
121         data[size] = null; // loitering objects != memory leak
122         if (size == data.length/2) {
123             resize(data.length/2);
124         }
125         return ret;
126     }
127     // 从数组中删除第一个元素
128     public E removeFirst() {
129         return remove(0);
130     }
131     // 从元素中删除最后一个元素
132     public E removeLast() {
133         return remove(size-1);
134     }
135     // 从数组中删除元素e
136     public void removeElement(E e) {
137         int index = find(e);
138         if (index != -1) {
139             remove(index);
140         }
141     }
142 }

2. 数组数据结构的测试

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4 
 5         Array<Integer> array = new Array<Integer>();
 6         for (int i = 0; i < 10; i++) {
 7             array.addLast(i);
 8         }
 9         System.out.println(array);
10         System.out.println("在数组索引1处插入:200。此时数组会扩容为原来的2倍。");
11         array.add(1,200);
12         System.out.println(array);
13         System.out.println("在数组最后插入:1");
14         array.addLast(1);
15         System.out.println(array);
16         System.out.println("在数组开头插入:-1");
17         array.addFirst(-1);
18         System.out.println(array);
19         System.out.println("移除数组开头处的元素。");
20         array.removeFirst();
21         System.out.println("移除数组最后的元素。");
22         array.removeLast();
23         System.out.println(array);
24         System.out.println("移除数组索引1处的元素。此时数组会缩容为原来的1/2倍。");
25         array.remove(1);
26         System.out.println(array);
27         System.out.println("数组中是否包含元素:8。返回:true");
28         System.out.println(array.contains(8));
29         System.out.println("查询元素8所在位置的索引。返回:8");
30         System.out.println(array.find(8));
31         System.out.println("查询索引位置为8的元素。返回:8");
32         System.out.println(array.get(8));
33     }
34 }

3. 测试结果

Array: size = 10, capacity = 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
在数组索引1处插入:200。此时数组会扩容为原来的2倍。
Array: size = 11, capacity = 20
[0, 200, 1, 2, 3, 4, 5, 6, 7, 8, 9]
在数组最后插入:1
Array: size = 12, capacity = 20
[0, 200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
在数组开头插入:-1
Array: size = 13, capacity = 20
[-1, 0, 200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
移除数组开头处的元素。
移除数组最后的元素。
Array: size = 11, capacity = 20
[0, 200, 1, 2, 3, 4, 5, 6, 7, 8, 9]
移除数组索引1处的元素。此时数组会缩容为原来的1/2倍。
Array: size = 10, capacity = 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
数组中是否包含元素:8。返回:true
true
查询元素8所在位置的索引。返回:8
8
查询索引位置为8的元素。返回:8
8

 时间复杂度:常数阶O(1),对数阶O(log2n),线性阶O(n),线性对数阶O(nlog2n),平方阶O(n^2),立方阶O(n^3),指数阶O(2^n)

猜你喜欢

转载自www.cnblogs.com/a-yuan/p/10265252.html
01