国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:16日期: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ù)組方式

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

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

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

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

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

2、數(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;}

數(shù)組添加元素:數(shù)組空間不夠就會擴(kuò)容(原來空間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)造方法,默認(rèn)初始容量為capacity=10 public Array() { this(10); } // 獲取數(shù)組中元素個(gè)數(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ù)組第一個(gè)元素,返回刪除的元素 public E removeFirst() { return remove(0); } // 刪除數(shù)組最后一個(gè)元素 public E removeLast() { return remove(size - 1); } // 刪除數(shù)組中指定元素e 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; } // 重寫父類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個(gè)元素到數(shù)組之后,然后再添加一個(gè)元素,此時(shí)數(shù)組會擴(kuò)容為原來空間的兩倍。

添加數(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

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

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

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

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

補(bǔ)充:

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

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

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 久久亚洲精品无码观看不卡 | 亚洲精品久久久久综合中文字幕 | 91在线国产观看 | 波多野结衣视频在线观看地址免费 | 九九久久精品 | 久久欧美精品欧美久久欧美 | 国产精品aⅴ| 亚洲国产日韩女人aaaaaa毛片在线 | 国产成人在线视频网站 | 亚洲一区二区三区欧美 | 日韩在线观看视频免费 | 91精品国产爱久久久久 | 日本在线观看免费视频网址 | 全部在线播放免费毛片 | 国产三级a三级三级 | 久久伊人男人的天堂网站 | 精品国产91久久久久 | 在线观看日本www | 亚洲第一区精品日韩在线播放 | 一级aaa级毛片午夜在线播放 | 狼人青草久久网尹人 | 亚洲欧洲一二三区机械有限公司 | 色综合精品久久久久久久 | 一级午夜a毛片免费视频 | 在线综合+亚洲+欧美中文字幕 | 国产精品久久久久亚洲 | 国产91会所洗浴女技师按摩 | 在线成人免费 | 成年人看的免费视频 | 中文字幕亚洲精品日韩精品 | 深夜在线观看大尺度 | 国产成人美女福利在线观看 | 免费久久 | 美女精品永久福利在线 | 午夜香港三级a三级三点 | 欧美成人亚洲 | 亚洲在线成人 | 亚洲综合91社区精品福利 | 一级毛片欧美大片 | 亚洲人成在线免费观看 | 国产精品久久久久精 |