二次封装自己的数组类
package cn.ninini.array;
public class Array<E> {
private E[] data;
private int size;
/**
* 构造方法
*
* @param capacity
* 传入数组容量
*/
@SuppressWarnings("unchecked")
public Array(int capacity) {
data = (E[]) new Object[capacity];
size = 0;
}
/**
* 无参构造方法,初始容量10
*/
public Array() {
this(10);
}
/**
* 获取元素个数
*
* @return 元素个数
*/
public int getSize() {
return size;
}
/**
* 获取数组容量
*
* @return 容量大小
*/
public int getCapacity() {
return data.length;
}
/**
* 返回数组是否为空
*
* @return 为空返回true,不为空返回false
*/
public boolean isEmpty() {
return size == 0;
}
/**
* 向所有元素末尾添加元素
*
* @param e
* 待添加元素
*/
public void addLast(E e) {
add(size, e);
}
/**
* 在所有元素前添加一个新元素
*
* @param e
* 待添加元素
*/
public void addFirst(E e) {
add(0, e);
}
/**
* 向第index插入元素e
*
* @param index
* 插入位置
* @param e
* 待插入元素
*/
public void add(int index, E e) {
if (index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
if (size == data.length)
resize(2 * data.length);
for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];
data[index] = e;
size++;
}
/**
* 获取指定位置元素
*
* @param index
* 指定位置
* @return 指定位置元素
*/
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal!");
}
return data[index];
}
/**
* 修改指定位置元素
*
* @param index
* 指定位置
* @param e
* 修改元素值
*/
public void set(int index, E e) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Index is illegal.");
data[index] = e;
}
/**
* 查找数组中是否有元素e
*
* @param e
* 带查找元素
* @return 存在返回true,不存在返回false
*/
public boolean contains(E e) {
for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return true;
}
}
return false;
}
/**
* 从数组中删除元素e
*
* @param e
* 待删除元素
* @return 成功返回true,失败返回false
*/
public boolean removeElement(E e) {
int index = find(e);
if (index != -1) {
remove(index);
return true;
}
return false;
}
/**
* 查找指定元素位置
*
* @param e
* 待查找元素
* @return 存在返回元素位置,不存在返回-1
*/
public int find(E e) {
for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
/**
* 删除指定位置元素
*
* @param index
* 指定位置
* @return 被删除元素
*/
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Remove failed. Index is illegal!");
}
E ret = data[index];
for (int i = index + 1; i < size; i++)
data[i - 1] = data[i];
size--;
data[size] = null;
// 防止复杂度震荡
if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return ret;
}
/**
* 删除首元素
*
* @return 被删除首元素
*/
public E removeFirst() {
return remove(0);
}
/**
* 删除尾元素
*
* @return 被删除尾元素
*/
public E removeLast() {
return remove(size - 1);
}
/**
* 将数组空间的容量变成newCapacity大小
*
* @param newCapacity
* 新容量
*/
@SuppressWarnings("unchecked")
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1)
res.append(", ");
}
res.append(']');
return res.toString();
}
}