用来存储一组相同数据类型的数
- 线性结构
- 有索引
- 查询快
- 增删慢
数组在内存中分配的是连续的空间
MyselfArray
package array.classdemo;
/**
* 定义显示自己的数组
*
* @author Deevan
*/
public class MyselfArray<T> {
/**
* 数据容器
*/
private T[] data;
/**
* 保存数据的实际个数
*/
private int size;
public MyselfArray() {
//没有参数就创建一个容量为20的数组
this(20);
}
public MyselfArray(int capacity) { //定义参数capacity:容器的容量
this.size = 0;
data = (T[]) new Object[capacity];
}
/**
* 判断数组是否为空
*/
public boolean isEmpty() {
return this.size == 0;
}
/**
* 获取数组实际保存元素的个数
*
* @return 长度
*/
public int getSize() {
return this.size;
}
/**
* 向数组尾部添加元素
*
* @param ele 元素内容
*/
public void addTail(T ele) {
addIndex(this.size, ele);
}
/**
* 在头部添加元素
*
* @param ele 元素内容
*/
public void addHead(T ele) {
addIndex(0, ele);
}
/**
* 在指定位置添加元素
*
* @param index 指定位置
* @param ele 元素内容
*/
public void addIndex(int index, T ele) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("index out of dataRange");
}
//判断index是否满足,满了则扩容为原来的两倍
if (this.size == data.length) {
resize(2 * data.length);
}
//把index和以后的所有元素从后向前都往后移一位
for (int i = this.size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = ele;
this.size++;
}
/**
* 更改数组的容积
*
* @param newCapacity 新容积
*/
public void resize(int newCapacity) {
//创建一个长度为newCapacity的新数组
T[] newData = (T[]) new Object[newCapacity];
//遍历Data中的元素添加到newData中
for (int i = 0; i < this.size; i++) {
newData[i] = this.data[i];
}
this.data = newData;
}
/**
* 获取指定位置的元素内容
*
* @param index 指定位置
* @return int
*/
public T getEleByIndex(int index) {
if (index < 0 || index > this.size) {
throw new IllegalArgumentException("index is error");
}
return data[index];
}
/**
* 修改指定位置的元素
*
* @param index 索引
* @param ele 内容
*/
public void updateByIndex(int index, T ele) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("index is error");
}
data[index] = ele;
}
/**
* 是否包含指定的元素内容
*
* @param ele 内容
* @return 该元素索引 / -1
*/
public int isContains(T ele) {
for (int i = 0; i < this.size; i++) {
if (this.data[i] == ele) {
return i;
}
}
return -1;
}
/**
* 删除指定位置的元素
*
* @param index 索引
* @return int
*/
public T remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("index is invalid!");
}
T result = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
this.size--;
data[size] = null;
// 进行缩容操作
if (this.size == data.length / 4 && data.length / 4 > 0) {
resize(data.length / 2);
}
return result;
}
/**
* 移除头部元素
*
* @return int
*/
public T removeHead() {
return remove(0);
}
/**
* 移除尾部元素
*
* @return int
*/
public T removeTail() {
return remove(this.size - 1);
}
/**
* 删除指定的元素
*
* @param ele 内容
* @return 索引
*/
public int removeEle(T ele) {
int index = isContains(ele);
if (index != -1) {
remove(index);
}
return index;
}
/**
* 重写输出方法把数组以String类型输出
*
* @return String
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < this.size; i++) {
sb.append(data[i]);
if (i != this.size - 1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
}
时间复杂度分析
时间复杂度我们通常考虑最糟糕的情况
- 增:O(n)
- 删:O(n)
- 改:已知索引O(1);未知索引O(n)
- 查:已知索引O(1);未知索引O(n)