亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Java 自定義動態(tài)數(shù)組方式

瀏覽:9日期:2022-08-15 10:09:31
Java自定義動態(tài)數(shù)組1、靜態(tài)數(shù)組向動態(tài)數(shù)組轉(zhuǎn)變

(1)靜態(tài)數(shù)組,數(shù)組空間固定長度

Java 自定義動態(tài)數(shù)組方式

這個數(shù)組空間總長為4,如果此時新插入一個數(shù)據(jù)就會報數(shù)組空間不足

(2)靜態(tài)數(shù)組如何轉(zhuǎn)變成動態(tài)數(shù)組

Java 自定義動態(tài)數(shù)組方式

第一步:創(chuàng)建一個空間是data數(shù)組兩倍的newData數(shù)組(擴容);

第二步:把data數(shù)組中的元素全部賦值到newData數(shù)組;

2、數(shù)組擴容程序

// 數(shù)組擴容private void resize(int newCapacity){ E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData;}

數(shù)組添加元素:數(shù)組空間不夠就會擴容(原來空間2倍)

// 數(shù)組指定位置添加元素 public void add(int index, E e) {// if (size == data.length)// throw new IllegalArgumentException('Add failed.Array is full.'); if (index < 0 || index > size) throw new IllegalArgumentException('Add failed. Require index >= 0 and index <= size'); if (size == data.length) resize(2 * data.length); for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; data[index] = e; size++; }

數(shù)組刪除元素:數(shù)組空間空閑太大就會縮容(原來空間的1/2)

// 從數(shù)組中刪除index位置的元素,返回刪除的元素public E remove(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException('Remove failed.Index is illegal'); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; // loitering objects != memory leak 手動釋放內(nèi)存空間 data[size] = null; if(size == data.length / 2) { resize(data.length / 2); } return ret;}3、數(shù)組整體代碼

public class Array<E> { // 定義數(shù)組變量,data.length表示數(shù)組容量capacity private E[] data; // 定義數(shù)組中存放數(shù)據(jù)大小 private int size; // 有參構(gòu)造方法,傳入數(shù)組的容量capacity構(gòu)造動態(tài)數(shù)組 public Array(int capacity) { data = (E[])new Object[capacity]; size = 0; } // 無參構(gòu)造方法,默認初始容量為capacity=10 public Array() { this(10); } // 獲取數(shù)組中元素個數(shù) public int getSize() { return size; } // 獲取數(shù)組的容量 public int getCapacity() { return data.length; } // 判斷數(shù)組是否為空 public boolean isEmpty() { return size == 0; }/* // 在數(shù)組末尾添加元素 public void addLast(E e) { if (size == data.length) throw new IllegalArgumentException('AddLast failed.Array is full.'); data[size] = e; size++; }*/ // 在數(shù)組末尾添加元素(復(fù)用add方法) public void addLast(E e) { add(size, e); } // 在數(shù)組頭部添加元素(復(fù)用add方法) public void addFirst(E e) { add(0, e); } // 數(shù)組指定位置添加元素 public void add(int index, E e) {// if (size == data.length)// throw new IllegalArgumentException('Add failed.Array is full.'); if (index < 0 || index > size) throw new IllegalArgumentException('Add failed. Require index >= 0 and index <= size'); if (size == data.length) resize(2 * data.length); for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; data[index] = e; size++; } // 獲取index索引位置的元素 public E get(int index) { if (index < 0) { throw new IllegalArgumentException('Get failed.Index is illegal.'); } return data[index]; } // 修改index索引位置的元素 public void set(int index, E e) { if (index < 0 || index >= size) { throw new IllegalArgumentException('Set failed.Index is illegal.'); } 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) { if (index < 0 || index >= size) { throw new IllegalArgumentException('Remove failed.Index is illegal'); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; // loitering objects != memory leak 手動釋放內(nèi)存空間 data[size] = null; if(size == data.length / 2) { resize(data.length / 2); } return ret; } // 刪除數(shù)組第一個元素,返回刪除的元素 public E removeFirst() { return remove(0); } // 刪除數(shù)組最后一個元素 public E removeLast() { return remove(size - 1); } // 刪除數(shù)組中指定元素e public void removeElement(E e) { int index = find(e); if (index != -1) { remove(index); } } // 數(shù)組擴容 private void resize(int newCapacity){ E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; } // 重寫父類toString()方法 @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(String.format('Array: size = %d , capacity = %dn', size, data.length)); sb.append(’[’); for (int i = 0; i < size; i++) { sb.append(data[i]); if (i != size - 1) {sb.append(’,’); } } sb.append(’]’); return sb.toString(); }}4、數(shù)組測試代碼

public class ArrayTest { public static void main(String[] args) { // 測試toString()方法 Array<Integer> arr = new Array(10); for (int i = 0; i < 10; i++) { // 測試addLast(int e)方法 arr.addLast(i); } System.out.println('添加數(shù)組元素:'); System.out.println(arr); // 測試add(int index, int e)方法 arr.add(1, 200); System.out.println('在數(shù)組指定索引位置插入元素e:'); System.out.println(arr); // 測試addFirst(int e)方法 arr.addFirst(-10); System.out.println('在數(shù)組頭部位置插入元素e:'); System.out.println(arr); }}

測試結(jié)果如下所示:初始化數(shù)組空間大小為10,第一次插入10個元素到數(shù)組之后,然后再添加一個元素,此時數(shù)組會擴容為原來空間的兩倍。

添加數(shù)組元素:

Array: size = 10 , capacity = 10[0,1,2,3,4,5,6,7,8,9]

在數(shù)組指定索引位置插入元素e:

Array: size = 11 , capacity = 20[0,200,1,2,3,4,5,6,7,8,9]

在數(shù)組頭部位置插入元素e:

Array: size = 12 , capacity = 20

補充:Java靜態(tài)數(shù)組和動態(tài)數(shù)組的定義方式

數(shù)組的定義方式靜態(tài):

//簡化語法常用 定義和初始化同步完成int [] a = {5,2,6,4,10};動態(tài):

//數(shù)組的定義和初始化同時完成,使用動態(tài)初始化語法int[] prices = new int[5];

補充:

//初始化數(shù)組時元素的類型是定義數(shù)組時元素類型的子類Object[] books = new String[4];

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产精品久久久久国产精品三级 | 黄色毛片在线看 | 亚洲自偷自偷精品 | 欧美一区二区三区在线视频 | 久久久99精品免费观看精品 | 国产精品久久久久久久久久一区 | 一级毛片大全 | 国产一级黄色网 | 国产1级片 | 精品一区二区三区在线观看视频 | 国产成人精品aaaa视频一区 | 久久这里只有精品首页 | 九九免费高清在线观看视频 | 九九视频高清视频免费观看 | 成人午夜免费视频毛片 | 在线观看国产小屁孩cao大人 | 欧美唯爱网 全黄性播放 | 免费视频久久看 | 色婷婷5月精品久久久久 | 中文一级黄色片 | 国产三级91 | 日韩在线免费视频观看 | 免费国产免费福利视频 | 午夜视频网站在线观看 | 在线观看国产欧美 | 久久黄色网址 | 亚洲不卡在线 | 9i9精品国产免费久久 | 香蕉香蕉国产片一级一级毛片 | 国产片毛片 | 午夜国产精品不卡在线观看 | 亚洲欧美日韩综合一区 | 久久久久欧美国产精品 | 国产欧美国产精品第二区 | 国产美女亚洲精品久久久综合 | 免费在线国产视频 | 欧美日韩一区二区亚洲 | 中文字幕一区二区在线观看 | 国产精品一二区 | 久草久操| 小明看片成人永久在线观看 |