數(shù)組有上界和下界肃拜,數(shù)組的元素在上下界內(nèi)是連續(xù)的。
數(shù)組的特點(diǎn)是:數(shù)據(jù)是連續(xù)的雌团;隨機(jī)訪問速度快燃领。數(shù)組中稍微復(fù)雜一點(diǎn)的是多維數(shù)組和動(dòng)態(tài)數(shù)組。至于動(dòng)態(tài)數(shù)組锦援,是指數(shù)組的容量能動(dòng)態(tài)增長的數(shù)組猛蔽;Collection集合中提供了ArrayList和Vector。
package net.good.spring;
public class Array<E> {
private E[] data;
private int size;
// Array構(gòu)造函數(shù)灵寺。capacity初始化容量大小
public Array(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Illegal Capacity: " + capacity);
}
this.data = (E[]) new Object[capacity];
this.size = 0;
}
// Array構(gòu)造函數(shù)曼库。默認(rèn)容量是10。
public Array() {
this(10);
}
// 獲取數(shù)組中個(gè)元素個(gè)數(shù)
public int getSize() {
return this.size;
}
// 返回?cái)?shù)組是否為空
public boolean isEmpty() {
return this.size == 0;
}
// 獲取數(shù)組的容量
public int getCapacity() {
return this.data.length;
}
// 在第index個(gè)位置插入一個(gè)新的元素e
public E[] add(int index, E e) {
checkAddRange(index);
ensureCapacity(this.size + 1);
System.arraycopy(this.data, index, this.data, index + 1, this.size - index);
this.data[index] = e;
this.size++;
return this.data;
}
// 插入一個(gè)新的元素e
public E[] add(E e) {
return add(this.size, e);
}
// 確定Arrar的容量略板。
public void ensureCapacity(int capacity) {
if (capacity >= getCapacity()) {
// 若當(dāng)前容量不足以容納當(dāng)前的元素個(gè)數(shù)毁枯,設(shè)置 新的容量=“(原始容量x2) + 1”
int newCapacity = getCapacity() * 2 + 1;
if (capacity > newCapacity) {
newCapacity = capacity;
}
E[] newData = (E[]) new Object[newCapacity];
System.arraycopy(this.data, 0, newData, 0, this.size);
this.data = newData;
}
}
public void checkAddRange(int index) {
if (index < 0 || index > this.size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
}
}
public void checkRange(int index) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
}
}
// 獲取index位置的元素值
public E get(int index) {
checkRange(index);
return this.data[index];
}
// 設(shè)置index位置的值為element
public void set(int index, E e) {
checkRange(index);
this.data[index] = e;
}
//是否包含element
public boolean contains(E e) {
return indexOf(e) >= 0;
}
// 正向查找,返回元素的索引值
public int indexOf(E e) {
if (e == null) {
for (int i = 0; i < this.size; i++) {
if (this.data[i] == null) {
return i;
}
}
} else {
for (int i = 0; i < this.size; i++) {
if (e.equals(this.data[i])) {
return i;
}
}
}
return -1;
}
// 反向查找叮称,返回元素的索引值
public int lastIndexOf(E e) {
if (e == null) {
for (int i = this.size - 1; i >= 0; i--) {
if (this.data[i] == null) {
return i;
}
}
} else {
for (int i = this.size - 1; i >= 0; i--) {
if (e.equals(this.data[i])) {
return i;
}
}
}
return -1;
}
//刪除指定index的元素
public E remove(int index) {
checkRange(index);
E oldValue = this.data[index];
int numMoved = size - index - 1;
if (numMoved > 0){
System.arraycopy(this.data, index+1, this.data, index, numMoved);
}
this.data[--this.size] = null;
return oldValue;
}
//刪除指定元素
public int remove(E e){
int index = indexOf(e);
if (index > 0) {
remove(index);
}
return index;
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append('[');
for(int i = 0; i < size; i++) {
res.append(data[i]);
if(i != size - 1)
res.append(", ");
}
res.append(']');
return res.toString();
}
}
參考:skywang12345
哈哈大圣