众所周知,在Java中,数组的特点是
> 数组是用来存储一组有序的数据的容器 > 数组中存储数据的地址是连续的 > 数组中存储数据的类型是一致的 > 数组一旦创建、长度不可改变
因此,数组也有缺点,比如说,数组一旦创建,长度不可改变。这样的话,就不怎么利于我们的使用。我们学习的数组都是静态数组,其实在很多的时候,静态数组根本不能满足我们编程的实际需要,比方说我们需要在程序运行过程中动态的向数组中添加数据,这时我们的静态数组大小是固定的,显然就不能添加数据,要动态添加数据必须要用到动态数组,动态数组中的各个元素类型也是一致的。
在我们的学习使用中,经常要对数组进行 增加、删除、改变元素、查找元素等等...一系列操作。所以普通的数组已经满足不了我们的需求,哪有什么办法可以解决这个问题?在这里,我们就要用到动态数组。
动态数组类的主体
public class IntArray {
/**
* 作用是用来存储 真正的数据的
*/
private int[] array = new int[10];
/**
* 存储 数组的真实 有效长度
*/
private int size;
/**
* 创建一个空的对象
*/
public IntArray() {
}
/**
* 将数组转成动态数组
*
* @param array
*/
public IntArray(int[] array) {
this.array = array;
this.size = array.length;
}
/**
* 获取动态数组的有效长度
*
* @return
*/
public int size() {
return this.size;
}
}
对动态数组添加方法
向动态数组的指定位置添加元素
/**
* 向动态数组的指定位置添加元素
*
* @param index
* @param ele
*/
public void add(int index, int ele) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException("数组超出了IntArray的范围,期待的值是0~" + (size - 1) + ",实际传入的是" + index);
// 尝试扩容
grow();
// 从 index 位置 开始 ,所有后面的元素都要进行移动
for (int i = size - 1; i >= index; i--) {
array[i + 1] = array[i];
}
// 在 index位置插入元素
array[index] = ele;
// 有效长度 +1
size++;
}
删除指定位置的元素,并返回删除的元素
/**
* 定义一个方法、用来删除指定位置的元素,并返回被删除的元素
*/
public int remove(int index) {
// 检查索引
checkIndex(index);
// 删除指定位置的元素
// 获取 index 位置的元素
int ele = get(index);
// 从删除元素的位置开始 移动后面所有的元素
for (int i = index; i < size - 1; i++) {
array[i] = array[i + 1];
}
// 将 有效长度 -1
array[size - 1] = 0;
size--;
return ele;
}
修改动态数组 指定位置的元素
/**
* 修改动态数组 指定位置的元素
*
* @param index
* @param ele
*/
public void set(int index, int ele) {
// 检查索引
checkIndex(index);
// 修改 index 的位置元素为 ele
array[index] = ele;
}
删除指定的第一个元素,移除失败、返回false
/**
* 删除指定的第一个元素,移除失败、返回false
*
* @param ele
* @return
*/
public boolean removeElement(int ele) {
// 获取 ele 元素出现的位置
int index = indexOf(ele);
if (index == -1) return false;
// 删除 index 位置的元素
remove(index);
return true;
}
删除指定个数的元素
count > 0 : 从前删除 count个 ele元素
count = 0 : 删除所有 ele元素
count <0 : 从后删除 count 个 ele 元素
/**
* void removeElement(int ele , int count) : 删除指定个数的元素
* count > 0 : 从前删除 count个 ele元素
* count = 0 : 删除所有 ele元素
* count <0 : 从后删除 count 个 ele 元素
*
* @param ele
* @param count
*/
public void removeElement(int ele, int count) {
// 如果 count >=0 , 则 删除 指定个数的元素(从前到后)
int n = 0;
if (count >= 0) {
for (int i = 0; i < size; i++) {
if (ele == array[i]) {
// 删除 第 i 个位置的元素
remove(i--);
if (++n == count) return;
}
}
} else {
for (int i = size - 1; i >= 0; i--) {
if (ele == array[i]) {
remove(i);
if (--n == count) return;
}
}
}
}
获取 指定位置的元素
/**
* 根据索引 获取 对应位置的元素
*
* @param index
* @return
*/
public int get(int index) {
// 检查索引
checkIndex(index);
return array[index];
}
动态数组的扩容
/**
* 内部使用的扩容
*/
private void grow() {
if (size >= array.length) {
// 创建一个新数组
int[] newarray = new int[array.length << 1];
// 将 原数组中的有效内容拷贝到新数组中
System.arraycopy(array, 0, newarray, 0, size);
// 将 新数组 赋值给 当前类的 array 属性
this.array = newarray;
}
}
toString方法的重写
public String toString() {
if (array == null)
return "null";
int iMax = size - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(array[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
加入上述方法,动态数组的基本功能就可以实现。后期大家在使用的时候,还可以添加自己,需要的方法。