|
| 1 | +package com.skyl.Array04; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * 泛型数组,可自动扩容 |
| 6 | + * @param <E> |
| 7 | + */ |
| 8 | +public class Array<E> { |
| 9 | + |
| 10 | + private E[] data; |
| 11 | + private int size; |
| 12 | + |
| 13 | + // 构造函数,传入数组的容量capacity构造Array |
| 14 | + public Array(int capacity){ |
| 15 | + data = (E[])new Object[capacity]; |
| 16 | + size = 0; |
| 17 | + } |
| 18 | + |
| 19 | + // 无参数的构造函数,默认数组的容量capacity=10 |
| 20 | + public Array(){ |
| 21 | + this(10); |
| 22 | + } |
| 23 | + |
| 24 | + // 获取数组的容量 |
| 25 | + public int getCapacity(){ |
| 26 | + return data.length; |
| 27 | + } |
| 28 | + |
| 29 | + // 获取数组中的元素个数 |
| 30 | + public int getSize(){ |
| 31 | + return size; |
| 32 | + } |
| 33 | + |
| 34 | + // 返回数组是否为空 |
| 35 | + public boolean isEmpty(){ |
| 36 | + return size == 0; |
| 37 | + } |
| 38 | + |
| 39 | + // 在index索引的位置插入一个新元素e |
| 40 | + public void add(int index, E e){ |
| 41 | + |
| 42 | + if(index < 0 || index > size) |
| 43 | + throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size."); |
| 44 | + |
| 45 | + if(size == data.length) |
| 46 | + resize(2 * data.length); |
| 47 | + |
| 48 | + for(int i = size - 1; i >= index ; i --) |
| 49 | + data[i + 1] = data[i]; |
| 50 | + |
| 51 | + data[index] = e; |
| 52 | + |
| 53 | + size ++; |
| 54 | + } |
| 55 | + |
| 56 | + // 向所有元素后添加一个新元素 |
| 57 | + public void addLast(E e){ |
| 58 | + add(size, e); |
| 59 | + } |
| 60 | + |
| 61 | + // 在所有元素前添加一个新元素 |
| 62 | + public void addFirst(E e){ |
| 63 | + add(0, e); |
| 64 | + } |
| 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 | + return data[index]; |
| 71 | + } |
| 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 | + data[index] = e; |
| 78 | + } |
| 79 | + |
| 80 | + // 查找数组中是否有元素e |
| 81 | + public boolean contains(E e){ |
| 82 | + for(int i = 0 ; i < size ; i ++){ |
| 83 | + if(data[i].equals(e)) |
| 84 | + return true; |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1 |
| 90 | + public int find(E e){ |
| 91 | + for(int i = 0 ; i < size ; i ++){ |
| 92 | + if(data[i].equals(e)) |
| 93 | + return i; |
| 94 | + } |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + // 从数组中删除index位置的元素, 返回删除的元素 |
| 99 | + public E remove(int index){ |
| 100 | + if(index < 0 || index >= size) |
| 101 | + throw new IllegalArgumentException("Remove failed. Index is illegal."); |
| 102 | + |
| 103 | + E ret = data[index]; |
| 104 | + for(int i = index + 1 ; i < size ; i ++) |
| 105 | + data[i - 1] = data[i]; |
| 106 | + size --; |
| 107 | + data[size] = null; // loitering objects != memory leak |
| 108 | + |
| 109 | + if(size == data.length / 2) |
| 110 | + resize(data.length / 2); |
| 111 | + return ret; |
| 112 | + } |
| 113 | + |
| 114 | + // 从数组中删除第一个元素, 返回删除的元素 |
| 115 | + public E removeFirst(){ |
| 116 | + return remove(0); |
| 117 | + } |
| 118 | + |
| 119 | + // 从数组中删除最后一个元素, 返回删除的元素 |
| 120 | + public E removeLast(){ |
| 121 | + return remove(size - 1); |
| 122 | + } |
| 123 | + |
| 124 | + // 从数组中删除元素e |
| 125 | + public void removeElement(E e){ |
| 126 | + int index = find(e); |
| 127 | + if(index != -1) |
| 128 | + remove(index); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String toString(){ |
| 133 | + |
| 134 | + StringBuilder res = new StringBuilder(); |
| 135 | + res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length)); |
| 136 | + res.append('['); |
| 137 | + for(int i = 0 ; i < size ; i ++){ |
| 138 | + res.append(data[i]); |
| 139 | + if(i != size - 1) |
| 140 | + res.append(", "); |
| 141 | + } |
| 142 | + res.append(']'); |
| 143 | + return res.toString(); |
| 144 | + } |
| 145 | + |
| 146 | + // 将数组空间的容量变成newCapacity大小 |
| 147 | + private void resize(int newCapacity){ |
| 148 | + |
| 149 | + E[] newData = (E[])new Object[newCapacity]; |
| 150 | + for(int i = 0 ; i < size ; i ++) |
| 151 | + newData[i] = data[i]; |
| 152 | + data = newData; |
| 153 | + } |
| 154 | +} |
0 commit comments