一个能够进行增删改查的数组的构建(数据结构01)
所需要的参数
数组的容量capacity,数组中所存元素的个数size
构造数组的步骤
首先对于这个数组,先对其进行变量的定义以及添加构造方法
public class Array01<E>{
//数组的大小
private int size;
//数组的容量
private int capacity;
//初始化数组
E[] data = (E[]) new Object[capacity];
//构造方法01
public Array01(int capacity) {
data = (E[]) new Object[capacity];
size = 0;
}
//初始化数组构造方法02
public Array01() {
// capacity = 10;
this(10);
size = 0;
}
}
接下来我们要写获取数组中所存储的元素的数量的方法getSize(),只需要返回size即可
public int getSize(){
return size;
}
获取数组的容量capacity的方法getCapacity(),其所需要返回的也是capacity
public int getCapacity(){
return capacity;
}
判断数组是否为空:isEmpty()
public boolean isEmpty() {
return size == 0;
}
接下来考虑在数组中任意位置添加一个元素:首先要考虑所输入的索引是否符合要求(在0~size-1之间),再考虑数组中是否有空位能够用于元素的添加,没有的话就要扩容。在对index位添加元素之前,先将index及其之后的元素均向后移动一位,再将所需要添加的元素赋值给index位
public void Addelement(int index, E e) {
//所插入的索引要在0~size-1之间
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add Failed,required index>=0&&index<size");
}
//被插入元素的数组不能填满数据,则需要扩容
if (size == data.length) {
resize(2 * data.length);
}
//把index及之后的元素均向后移动一位
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
之后考虑在数组元素的末尾和开头添加一个元素,直接调用上面的Addelement()方法
//在数组元素末尾添加一个元素
public void AddLast(E e) {
Addelement(size, e);
}
//在数组元素的开头添加一个新元素
public void AddFirst(E e) {
Addelement(0, e);
}
获取index索引处元素的方法getIndex(),首先依然要判断index是否是合法的数据
public E getIndex(int index) {
//判断index是否合法
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get Failed, Index is illegal");
}
return data[index];
}
修改index处的索引changeIndex(int Index E e)
//修改index索引处的元素
public void changeIndex(int index, E e) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get Failed, Index is illegal");
}
data[index] = e;
}
判断数组中是否存在某个元素的方法contain()和获取某个元素的索引的方法find(E e)
//判断数组中是否存在某个元素的方法
public boolean contain(E e) {
for (E i : data) {
if (i.equals(e)) {
return true;
}
}
return false;
}
//获取某个元素的索引
public int find(E e) {
for (int i = 0; i < size - 1; i++) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
对于一个数组,我们为了让其更加的合理利用计算即的资源,对数组的容量进行动态的调整resize(int new Capacity)
public void resize(int newCapacity) {
E[] newdata = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newdata[i] = data[i];
}
//把指针指向data
data = newdata;
}
对于删除元素,我们仿照之前添加元素,先要判断索引是否有效,之后考虑如果数组元素个数太少,也要调整数组的容量,删除元素即将index之后的元素(不含index位)向前移一位
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("the index is invalid");
}
E num = data[index];
for (int i = index + 1; i <= size - 1; i++) {
data[i - 1] = data[i];
}
data[size - 1] = null;
size--;
//完成删除操作后再决定是否需要缩小
if (size < data.length / 4 && data.length / 2 != 0) {
resize((data.length) / 2);
}
return num;
}
删除第一个元素以及最后一个元素或者删除数组中的某个具体的元素
//删除第一个元素
public E removeFirst() {
return remove(0);
}
//删除最后一个元素
public E removeLast() {
return remove(size - 1);
}
//在数组中删除某个元素
public void removeElement(E e) {
if (find(e) != -1) {
remove(find(e));
} else {
throw new IllegalArgumentException("the element is not in this array");
}
}
动态数组的toString()方法:
@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();
}
总的代码:
package Demo03;
public class Array01<E> {
//数组的大小
private int size;
//数组的容量
private int capacity;
E[] data = (E[]) new Object[capacity];
//构造方法
public Array01(int capacity) {
data = (E[]) new Object[capacity];
size = 0;
}
//初始化数组构造方法02
public Array01() {
// capacity = 10;
this(10);
size = 0;
}
//获取数组大小
public int getSize() {
return size;
}
//获取数组容量
public int getCapacity() {
return capacity;
}
//判断数组是否为空
public boolean isEmpty() {
return size == 0;
}
//对数组中任意位置添加一个元素
public void Addelement(int index, E e) {
//所插入的索引要在0~size-1之间
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add Failed,required index>=0&&index<size");
}
//被插入元素的数组不能填满数据(没有空位插入)
if (size == data.length) {
// throw new IllegalArgumentException("Add Fialed, the array is full ");
resize(2 * data.length);
}
//把index及之后的元素均向后移动一位
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
//在数组元素末尾添加一个元素
public void AddLast(E e) {
Addelement(size, e);
}
//在数组元素的开头添加一个新元素
public void AddFirst(E e) {
Addelement(0, e);
}
// capacity = data.length;
//获取index索引处的元素
public E getIndex(int index) {
//判断index是否合法
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get Failed, Index is illegal");
}
return data[index];
}
//修改index索引处的元素
public void changeIndex(int index, E e) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get Failed, Index is illegal");
}
data[index] = e;
}
//判断数组中是否存在某个元素的方法
public boolean contain(E e) {
for (E i : data) {
if (i.equals(e)) {
return true;
}
}
return false;
}
//获取某个元素的索引
public int find(E e) {
for (int i = 0; i < size - 1; i++) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
//在数组中删除指定索引的元素
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("the index is invalid");
}
E num = data[index];
for (int i = index + 1; i <= size - 1; i++) {
data[i - 1] = data[i];
}
data[size - 1] = null;
size--;
//完成删除操作后再决定是否需要缩小
if (size < data.length / 4 && data.length / 2 != 0) {
resize((data.length) / 2);
}
return num;
}
//删除第一个元素
public E removeFirst() {
return remove(0);
}
//删除最后一个元素
public E removeLast() {
return remove(size - 1);
}
//在数组中删除某个元素
public void removeElement(E e) {
if (find(e) != -1) {
remove(find(e));
} else {
throw new IllegalArgumentException("the element is not in this array");
}
}
//对数组扩容或者缩小的方法,其内涵是要创建一个新的两倍长的数组
public void resize(int newCapacity) {
E[] newdata = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newdata[i] = data[i];
}
//把指针指向data
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();
}
}