Java封裝數(shù)組之動態(tài)數(shù)組實(shí)現(xiàn)方法詳解
本文實(shí)例講述了Java封裝數(shù)組之動態(tài)數(shù)組實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
前言:在此之前,我們封裝的數(shù)組屬于靜態(tài)數(shù)組,也即數(shù)組空間固定長度,對于固定長度的數(shù)組當(dāng)元素超過容量時會報(bào)數(shù)組空間不足。為了能更好的使用數(shù)組,我們來實(shí)現(xiàn)一個可以自動擴(kuò)充容量的數(shù)組。
實(shí)現(xiàn)思路:
1.當(dāng)數(shù)組容量達(dá)到事先定義值時創(chuàng)建一個空間是data數(shù)組兩倍的newData數(shù)組(擴(kuò)容);
2.把data數(shù)組中的元素全部賦值到newData數(shù)組中;
3.把data數(shù)組重新執(zhí)行newData數(shù)組。
一、定義核心擴(kuò)容方法
// 數(shù)組擴(kuò)容
private void resize(int newCapacity){
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
二、改進(jìn)之前的數(shù)組添加元素方法(數(shù)組空間不夠時自動擴(kuò)容 --原理空間的2倍)
//在第index個位置插入一個新元素
public void add(int index, E e) {
//(1)判斷當(dāng)前需要插入值的位置是否合理,合理則轉(zhuǎn)入(3),否則拋出位置不合法異常
if (index < 0 || index > size)
throw new IllegalArgumentException("您選擇的位置不合法");
//(2)先判斷當(dāng)前數(shù)組容量是否已滿,滿則進(jìn)行容量擴(kuò)充
if (size == data.length)
resize(data.length * 2);
//將index位置之后的元素往后依次移動一位
for (int i = size - 1; i >= index; i--) {
//(3)將index之后的元素依次往后移動一位,然后將新元素插入到index位置
data[i + 1] = data[i];
}
data[index] = e;
//(4)維護(hù)size值
size++;
}
三、改進(jìn)之前的數(shù)組刪除元素方法(數(shù)組空間空閑太大就會縮容(原來空間的1/2))
//從數(shù)組中刪除index位置的元素,返回刪除的元素
public E remove(int index) {
//1.判斷索引的選擇是否合法
if (index < 0 || index > size)
throw new IllegalArgumentException("您選擇的位置不合法");
//2.先存儲需要刪除的索引對應(yīng)的值
E ret = data[index];
//將索引為index之后(index)的元素依次向前移動
for (int i = index + 1; i < size; i++) {
//3.執(zhí)行刪除--實(shí)質(zhì)為索引為index之后(index)的元素依次向前移動,將元素覆蓋
data[i - 1] = data[i];
}
//4.維護(hù)size變量
size--;
// loitering objects != memory leak 手動釋放內(nèi)存空間
data[size] = null;
if (size == data.length / 2) {
resize(data.length / 2);
}
//5.返回被刪除的元素
return ret;
}
通過以上,我們就可以實(shí)現(xiàn)一個動態(tài)的數(shù)組。
測試一下改進(jìn)后的代碼:
1.測試addLast()
DynamicArray<Integer> arr=new DynamicArray<Integer>(10);
for (int i = 0; i < 10; i++) {
arr.addLast(i);
}
System.out.println("添加數(shù)組元素:");
System.out.println(arr);
結(jié)果為:

2.測試add(int index,E e)方法
arr.add(1, 100);
System.out.println("在數(shù)組指定索引位置插入元素e:");
System.out.println(arr);
結(jié)果:

現(xiàn)在數(shù)組已經(jīng)從剛才定義的容量為10個變?yōu)榱巳萘繛?0個,數(shù)組中元素為11個,為此實(shí)現(xiàn)了數(shù)組擴(kuò)容。
3.測試removeLast方法
System.out.println("刪除數(shù)組最后一個元素:");
arr.removeLast();
System.out.println(arr);
結(jié)果為:

此時我們可以看出,刪除一個元素之后,數(shù)組容量又從新變?yōu)榱?0個。
本節(jié)所有代碼:
/**
* 3.動態(tài)數(shù)組
* 數(shù)組容量可變
*/
public class DynamicArray<E> {
//使用private 的目的是防止用戶從外界修改,造成數(shù)據(jù)不一致
private E[] data;
private int size;//數(shù)組中元素個數(shù)
//構(gòu)造函數(shù),傳入數(shù)組的容量capacity構(gòu)造Array函數(shù)
public DynamicArray(int capacity) {
data = (E[]) new Object[capacity];//泛型不能直接實(shí)例化
size = 0;
}
//無參構(gòu)造函數(shù),默認(rèn)數(shù)組的容量capacity=10
public DynamicArray() {
this(10);
}
//獲取數(shù)組中元素個數(shù)
public int getSize() {
return size;
}
//獲取數(shù)組的容量
public int getCapacity() {
return data.length;
}
//獲取數(shù)據(jù)是否為空
public boolean iEmpty() {
return size == 0;
}
//向所有元素后添加元素
public void addLast(E e) {
add(size, e);//size表示此時的最后一個元素
}
//在所有元素之前添加一個新元素
public void addFirst(E e) {
add(0, e);//0表示第一個位置
}
//在第index個位置插入一個新元素
public void add(int index, E e) {
//(1)判斷當(dāng)前需要插入值的位置是否合理,合理則轉(zhuǎn)入(3),否則拋出位置不合法異常
if (index < 0 || index > size)
throw new IllegalArgumentException("您選擇的位置不合法");
//(2)先判斷當(dāng)前數(shù)組容量是否已滿,滿則進(jìn)行容量擴(kuò)充
if (size == data.length)
resize(data.length * 2);
//將index位置之后的元素往后依次移動一位
for (int i = size - 1; i >= index; i--) {
//(3)將index之后的元素依次往后移動一位,然后將新元素插入到index位置
data[i + 1] = data[i];
}
data[index] = e;
//(4)維護(hù)size值
size++;
}
//獲取index索引位置的元素
public E get(int index) {
//(1)判斷當(dāng)前需要插入值的位置是否合理,合理則轉(zhuǎn)入(2),否則拋出位置不合法異常
if (index < 0 || index > size)
throw new IllegalArgumentException("您選擇的位置不合法");
//(2)返回索引index對應(yīng)的值
return data[index];
}
//獲取最后一個元素
public E getLast() {
return get(size - 1);
}
//獲取第一個元素
public E getFirst() {
return get(0);
}
//修改index索引位置的元素為e
void set(int index, E e) {
//(1)判斷當(dāng)前需要插入值的位置是否合理,合理則轉(zhuǎn)入(2),否則拋出位置不合法異常
if (index < 0 || index > size)
throw new IllegalArgumentException("您選擇的位置不合法");
//(2)修改索引index對應(yīng)的值
data[index] = e;
}
//查找數(shù)組中是否包含元素e
public boolean contains(E e) {
for (int i = 0; i < size; i++) {
if (data[i] == e)
return true;
}
return false;
}
//查找數(shù)組中元素e所在的索引(只是一個),如果不存在元素e,則返回-1;
public int find(E e) {
for (int i = 0; i < size; i++) {
if (data[i] == e)
return i;
}
return -1;
}
//從數(shù)組中刪除index位置的元素,返回刪除的元素
public E remove(int index) {
//1.判斷索引的選擇是否合法
if (index < 0 || index > size)
throw new IllegalArgumentException("您選擇的位置不合法");
//2.先存儲需要刪除的索引對應(yīng)的值
E ret = data[index];
//將索引為index之后(index)的元素依次向前移動
for (int i = index + 1; i < size; i++) {
//3.執(zhí)行刪除--實(shí)質(zhì)為索引為index之后(index)的元素依次向前移動,將元素覆蓋
data[i - 1] = data[i];
}
//4.維護(hù)size變量
size--;
// loitering objects != memory leak 手動釋放內(nèi)存空間
data[size] = null;
//縮容操作
if (size == data.length / 2 && data.length != 0) {
resize(data.length / 4);
}
//5.返回被刪除的元素
return ret;
}
//從數(shù)組中刪除第一個元素,返回刪除的元素
public E removeFirst() {
return remove(0);
}
//從數(shù)組中刪除最后一個元素,返回刪除的元素
public E removeLast() {
return remove(size - 1);
}
//從數(shù)組中刪除元素(只是刪除一個)
public void removeElement(E e) {
int index = find(e);
if (index != -1)
remove(index);
}
// 數(shù)組擴(kuò)容方法
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();
}
}
測試代碼:
public class test {
public static void main(String[] args) {
DynamicArray<Integer> arr=new DynamicArray<Integer>(10);
for (int i = 0; i < 10; i++) {
arr.addLast(i);
}
System.out.println("添加數(shù)組元素:");
System.out.println(arr);
arr.add(1, 100);
System.out.println("在數(shù)組指定索引位置插入元素e:");
System.out.println(arr);
System.out.println("刪除數(shù)組最后一個元素:");
arr.removeLast();
System.out.println(arr);
}
}
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
macOS上使用gperftools定位Java內(nèi)存泄漏問題及解決方案
這篇文章主要介紹了macOS上使用gperftools定位Java內(nèi)存泄漏問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例
這篇文章主要介紹了java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例,有需要的朋友可以參考一下2013-12-12
SpringBoot整合達(dá)夢數(shù)據(jù)庫的教程詳解
這篇文章主要給大家介紹了SpringBoot整合達(dá)夢數(shù)據(jù)庫的詳細(xì)教程,文章中有詳細(xì)的圖片介紹和代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-08-08
RabbitMQ消息隊(duì)列中多路復(fù)用Channel信道詳解
這篇文章主要介紹了RabbitMQ消息隊(duì)列中多路復(fù)用Channel信道詳解,消息Message是指在應(yīng)用間傳送的數(shù)據(jù),消息可以非常簡單,比如只包含文本字符串,也可以更復(fù)雜,可能包含嵌入對象,需要的朋友可以參考下2023-08-08
Java實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)提取所需知識點(diǎn)
這篇文章主要介紹了Java實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)提取所需知識點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

