Java封裝數組之改進為泛型數組操作詳解
本文實例講述了Java封裝數組之改進為泛型數組操作。分享給大家供大家參考,具體如下:
前言:通過上一節我們對我們需要封裝的數組,進行了基本的增刪改查的封裝,但只局限于int類型的操作,為了能提供多種類型數組的操作,我們可以將其進一步封裝為泛型數組。
1.定義泛型數組相關概念
(1)泛型數組讓我們可以存放任何數據類型
(2)存放的類型不可以是基本數據類型,只能是類對象
基本類型:
boolean、byte、char、short、int、long、float、double
(3)每個基本數據類型都有對應的包裝類
Boolean、Byte、Char、Short、Integer、Long、Float、Double
2.自定義泛型數組
/** * 2.泛型數組 */public class GenericArray<E> { //使用private 的目的是防止用戶從外界修改,造成數據不一致 private E[] data; private int size;//數組中元素個數 //構造函數,傳入數組的容量capacity構造Array函數 public GenericArray(int capacity) { data = (E[]) new Object[capacity];//泛型不能直接實例化 size = 0; } //無參構造函數,默認數組的容量capacity=10 public GenericArray() { this(10); } //獲取數組中元素個數 public int getSize() { return size; } //獲取數組的容量 public int getCapacity() { return data.length; } //獲取數據是否為空 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)先判斷當前數組容量是否已滿,未滿則轉入(2),否則拋出異常 if (size == data.length) { throw new IllegalArgumentException('數組已滿'); } //(2)判斷當前需要插入值的位置是否合理,合理則轉入(3),否則拋出位置不合法異常 if (index < 0 || index > size) { throw new IllegalArgumentException('您選擇的位置不合法'); } //將index位置之后的元素往后依次移動一位 for (int i = size - 1; i >= index; i--) { //(3)將index之后的元素依次往后移動一位,然后將新元素插入到index位置 data[i + 1] = data[i]; } data[index] = e; //(4)維護size值 size++; } //獲取index索引位置的元素 public E get(int index) { //(1)判斷當前需要插入值的位置是否合理,合理則轉入(2),否則拋出位置不合法異常 if (index < 0 || index > size) throw new IllegalArgumentException('您選擇的位置不合法'); //(2)返回索引index對應的值 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)判斷當前需要插入值的位置是否合理,合理則轉入(2),否則拋出位置不合法異常 if (index < 0 || index > size) throw new IllegalArgumentException('您選擇的位置不合法'); //(2)修改索引index對應的值 data[index] = e; } //查找數組中是否包含元素e public boolean contains(E e) { for (int i = 0; i < size; i++) { if (data[i] == e)return true; } return false; } //查找數組中元素e所在的索引(只是一個),如果不存在元素e,則返回-1; public int find(E e) { for (int i = 0; i < size; i++) { if (data[i] == e)return i; } return -1; } //從數組中刪除index位置的元素,返回刪除的元素 public E remove(int index) { //1.判斷索引的選擇是否合法 if (index < 0 || index > size) throw new IllegalArgumentException('您選擇的位置不合法'); //2.先存儲需要刪除的索引對應的值 E ret = data[index]; //將索引為index之后(index)的元素依次向前移動 for (int i = index + 1; i < size; i++) { //3.執行刪除--實質為索引為index之后(index)的元素依次向前移動,將元素覆蓋 data[i - 1] = data[i]; } //4.維護size變量 size--; // loitering objects != memory leak 手動釋放內存空間 data[size] = null; //5.返回被刪除的元素 return ret; } //從數組中刪除第一個元素,返回刪除的元素 public E removeFirst() { return remove(0); } //從數組中刪除最后一個元素,返回刪除的元素 public E removeLast() { return remove(size - 1); } //從數組中刪除元素(只是刪除一個) 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.測試泛型數組
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); }}
驗證結果如下:
更多關于java相關內容感興趣的讀者可查看本站專題:《Java數組操作技巧總結》、《Java字符與字符串操作技巧總結》、《Java數學運算技巧總結》、《Java數據結構與算法教程》及《Java操作DOM節點技巧總結》
希望本文所述對大家java程序設計有所幫助。
相關文章: