久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

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

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

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

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

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

Java 自定義動(dòng)態(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ù)組空間不夠就會(huì)擴(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ù)組空間空閑太大就會(huì)縮容(原來空間的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 手動(dòng)釋放內(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)造動(dòng)態(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 手動(dòng)釋放內(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ù)組測(cè)試代碼

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

測(cè)試結(jié)果如下所示:初始化數(shù)組空間大小為10,第一次插入10個(gè)元素到數(shù)組之后,然后再添加一個(gè)元素,此時(shí)數(shù)組會(huì)擴(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ù)組和動(dòng)態(tài)數(shù)組的定義方式

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

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

//數(shù)組的定義和初始化同時(shí)完成,使用動(dòng)態(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)文章:
主站蜘蛛池模板: 土默特左旗| 西贡区| 渭南市| 晋宁县| 镇沅| 河津市| 沙湾县| 平武县| 新蔡县| 东光县| 彭水| 汝阳县| 乌兰浩特市| 乐东| 德清县| 石河子市| 广州市| 基隆市| 山阴县| 平原县| 勐海县| 泽州县| 扎鲁特旗| 章丘市| 株洲县| 贡嘎县| 井陉县| 新疆| 杭锦旗| 铜山县| 滦平县| 讷河市| 柳河县| 土默特右旗| 新干县| 孟连| 金溪县| 南开区| 珠海市| 本溪| 余干县|