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

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

Java封裝數(shù)組之改進(jìn)為泛型數(shù)組操作詳解

瀏覽:3日期:2022-09-04 17:19:39

本文實(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é)果如下:

Java封裝數(shù)組之改進(jìn)為泛型數(shù)組操作詳解

更多關(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ì)有所幫助。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 欧美特黄一级视频 | 在线观看中文字幕亚洲 | 久久99国产亚洲高清观看首页 | 成人a毛片| 国产3级在线 | 免费永久国产在线视频 | 精品九九久久 | 国产情侣久久精品 | 国产精品久久久久久福利 | 波多野结衣一区在线观看 | 国产高清一级视频在线观看 | 一级片在线观看 | avtt制服丝袜 | 国产福利微拍精品一区二区 | 欧美三级不卡视频 | 国产成人精品区在线观看 | 日韩精品中文字幕在线 | 中文字幕在线无限2021 | 91精品久久久久含羞草 | 国产三级日本三级日产三 | 色黄网站aaaaaa级毛片 | 七七国产福利在线二区 | 亚洲精品一区二区在线播放 | 国产系列在线观看 | 特级毛片a级毛免费播放 | 国产亚洲精品久久久久久久久激情 | 成年女人毛片免费视频永久vip | 国产男女视频在线观看 | 亚洲成av人片在线观看无码 | 成人在线午夜 | 国产精品三级国语在线看 | 手机日韩理论片在线播放 | 日本乱人伦片中文字幕三区 | 国产精品久久久久久久9999 | 日本韩经典三级在线播放 | 国产特一级毛片 | 日韩中文字幕免费观看 | 一级片aaaa | 免费a级黄毛片 | 亚洲精品久久9热 | 亚洲天堂久久新 |