Java封裝數(shù)組之改進(jìn)為泛型數(shù)組操作詳解
本文實(shí)例講述了Java封裝數(shù)組之改進(jìn)為泛型數(shù)組操作。分享給大家供大家參考,具體如下:
前言:通過(guò)上一節(jié)我們對(duì)我們需要封裝的數(shù)組,進(jìn)行了基本的增刪改查的封裝,但只局限于int類(lèi)型的操作,為了能提供多種類(lèi)型數(shù)組的操作,我們可以將其進(jìn)一步封裝為泛型數(shù)組。
1.定義泛型數(shù)組相關(guān)概念
(1)泛型數(shù)組讓我們可以存放任何數(shù)據(jù)類(lèi)型
(2)存放的類(lèi)型不可以是基本數(shù)據(jù)類(lèi)型,只能是類(lèi)對(duì)象
基本類(lèi)型:
boolean、byte、char、short、int、long、float、double
(3)每個(gè)基本數(shù)據(jù)類(lèi)型都有對(duì)應(yīng)的包裝類(lèi)
Boolean、Byte、Char、Short、Integer、Long、Float、Double
2.自定義泛型數(shù)組
/** * 2.泛型數(shù)組 */public class GenericArray<E> { //使用private 的目的是防止用戶(hù)從外界修改,造成數(shù)據(jù)不一致 private E[] data; private int size;//數(shù)組中元素個(gè)數(shù) //構(gòu)造函數(shù),傳入數(shù)組的容量capacity構(gòu)造Array函數(shù) public GenericArray(int capacity) { data = (E[]) new Object[capacity];//泛型不能直接實(shí)例化 size = 0; } //無(wú)參構(gòu)造函數(shù),默認(rèn)數(shù)組的容量capacity=10 public GenericArray() { this(10); } //獲取數(shù)組中元素個(gè)數(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表示此時(shí)的最后一個(gè)元素 } //在所有元素之前添加一個(gè)新元素 public void addFirst(E e) { add(0, e);//0表示第一個(gè)位置 } //在第index個(gè)位置插入一個(gè)新元素 public void add(int index, E e) { //(1)先判斷當(dāng)前數(shù)組容量是否已滿(mǎn),未滿(mǎn)則轉(zhuǎn)入(2),否則拋出異常 if (size == data.length) { throw new IllegalArgumentException('數(shù)組已滿(mǎn)'); } //(2)判斷當(dāng)前需要插入值的位置是否合理,合理則轉(zhuǎn)入(3),否則拋出位置不合法異常 if (index < 0 || index > size) { throw new IllegalArgumentException('您選擇的位置不合法'); } //將index位置之后的元素往后依次移動(dòng)一位 for (int i = size - 1; i >= index; i--) { //(3)將index之后的元素依次往后移動(dòng)一位,然后將新元素插入到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對(duì)應(yīng)的值 return data[index]; } //獲取最后一個(gè)元素 public E getLast() { return get(size - 1); } //獲取第一個(gè)元素 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對(duì)應(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所在的索引(只是一個(gè)),如果不存在元素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.先存儲(chǔ)需要?jiǎng)h除的索引對(duì)應(yīng)的值 E ret = data[index]; //將索引為index之后(index)的元素依次向前移動(dòng) for (int i = index + 1; i < size; i++) { //3.執(zhí)行刪除--實(shí)質(zhì)為索引為index之后(index)的元素依次向前移動(dòng),將元素覆蓋 data[i - 1] = data[i]; } //4.維護(hù)size變量 size--; // loitering objects != memory leak 手動(dòng)釋放內(nèi)存空間 data[size] = null; //5.返回被刪除的元素 return ret; } //從數(shù)組中刪除第一個(gè)元素,返回刪除的元素 public E removeFirst() { return remove(0); } //從數(shù)組中刪除最后一個(gè)元素,返回刪除的元素 public E removeLast() { return remove(size - 1); } //從數(shù)組中刪除元素(只是刪除一個(gè)) public void removeElement(E e) { int index = find(e); if (index != -1) remove(index); } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append(String.format('Array:size=%d, capacity=%dn', 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(); }}
3.測(cè)試泛型數(shù)組
public class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } @Override public String toString() { return String.format('Student(name:%s, score:%d)', name, score); } public static void main(String[] args) { GenericArray<Student> studentArray = new GenericArray<>(); studentArray.addLast(new Student('test01', 66)); studentArray.addLast(new Student('test02', 77)); studentArray.addLast(new Student('test03', 88)); System.out.println(studentArray); }}
驗(yàn)證結(jié)果如下:
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. 原生js實(shí)現(xiàn)的觀察者和訂閱者模式簡(jiǎn)單示例2. asp讀取xml文件和記數(shù)3. JS錯(cuò)誤處理與調(diào)試操作實(shí)例分析4. JS實(shí)現(xiàn)表單中點(diǎn)擊小眼睛顯示隱藏密碼框中的密碼5. python基于scrapy爬取京東筆記本電腦數(shù)據(jù)并進(jìn)行簡(jiǎn)單處理和分析6. Python ellipsis 的用法詳解7. 在終端啟動(dòng)Python時(shí)報(bào)錯(cuò)的解決方案8. Python如何實(shí)現(xiàn)感知器的邏輯電路9. 基于android studio的layout的xml文件的創(chuàng)建方式10. PHP實(shí)現(xiàn)基本留言板功能原理與步驟詳解
