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

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

Java封裝數(shù)組實(shí)現(xiàn)包含、搜索和刪除元素操作詳解

瀏覽:3日期:2022-09-04 17:40:22

本文實(shí)例講述了Java封裝數(shù)組實(shí)現(xiàn)包含、搜索和刪除元素操作。分享給大家供大家參考,具體如下:

前言:在上一小節(jié)中我們已經(jīng)會(huì)了如何獲取和如何修改數(shù)組中的元素,在本小節(jié)中我們將繼續(xù)學(xué)習(xí)如何判斷某個(gè)元素是否在數(shù)組中存在、查詢出某個(gè)元素在數(shù)組中的位置、以及刪除數(shù)組中元素等方法的編寫。

1.查找數(shù)組中是否包含元素e,返回true或false

//查找數(shù)組中是否包含元素e public boolean contains(int e) { for (int i = 0; i < size; i++) { if (data[i] == e)return true; } return false; }

有時(shí)候在查詢過程中,我們不僅想知道是否包含該指定元素,還想是在該元素所在的位置,則我們可以編寫一個(gè)查找數(shù)組中元素e所在的索引的方法。

2.查找數(shù)組中元素e所在的索引(只是一個(gè)),如果不存在元素e,則返回-1。

//查找數(shù)組中元素e所在的索引(只是一個(gè)),如果不存在元素e,則返回-1; public int find(int e) { for (int i = 0; i < size; i++) { if (data[i] == e)return i; } return -1; }

3.從數(shù)組中刪除index位置的元素,返回刪除的元素

思路:

(1)判斷索引的選擇是否合法

(2)先存儲(chǔ)需要?jiǎng)h除的索引對(duì)應(yīng)的值

(3)執(zhí)行刪除--實(shí)質(zhì)為索引為index之后(index)的元素依次向前移動(dòng),將元素覆蓋。

(4)維護(hù)size變量

(5)返回被刪除的元素

//從數(shù)組中刪除index位置的元素,返回刪除的元素 public int remove(int index) { //1.判斷索引的選擇是否合法 if (index < 0 || index > size) throw new IllegalArgumentException('您選擇的位置不合法'); //2.先存儲(chǔ)需要?jiǎng)h除的索引對(duì)應(yīng)的值 int 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--; //5.返回被刪除的元素 return ret; }

有了刪除index位置的元素的方法,接下來,我們可以封裝一些其他的方法:

3.從數(shù)組中刪除第一個(gè)元素,返回刪除的元素

public int removeFirst() { return remove(0); }

4.從數(shù)組中刪除最后一個(gè)元素,返回刪除的元素

public int removeLast() { return remove(size - 1); }

在數(shù)組中刪除元素時(shí),除了通過索引的方式刪除之外,有時(shí)我們只知道需要?jiǎng)h除的元素是多少,而不知道具體的索引值,因此我們編寫一個(gè)通過元素值刪除的方法

5.從數(shù)組中刪除元素(只是刪除一個(gè))

//從數(shù)組中刪除元素(只是刪除一個(gè)) public void removeElement(int e) { int index = find(e); if (index != -1) remove(index); }

這里需要說明的是關(guān)于:

(1)從數(shù)組中刪除元素我們并不需要返回被刪除的元素,這是由于對(duì)于使用者來說,已經(jīng)知道自己要?jiǎng)h除的值是多少了,內(nèi)部無須在返回,

(2)針對(duì)通過索引方式刪除的元素需要返回被刪除,這是由于用戶并不知道自己刪除的元素值是什么,我們把被刪除的值返回給用戶,以便于用戶在需要使用時(shí)取用。

6.自定義數(shù)組方法測試驗(yàn)證

public class ArrayTest { public static void main(String[] args) { // 測試toString()方法 Array arr = new Array(20); 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); // 測試get(int index)方法 System.out.println('根據(jù)數(shù)組索引查找數(shù)組元素:'); System.out.println(arr.get(11)); // 測試set()方法 arr.set(11, 1000); System.out.println('修改數(shù)組索引位置上元素值:'); System.out.println(arr.get(11)); // 測試remove(index)方法 System.out.println(arr); arr.remove(0); System.out.println('刪除數(shù)組中指定index元素:'); System.out.println(arr); // 測試removeFist()方法 arr.removeFirst(); System.out.println('刪除數(shù)組中第一個(gè)元素:'); System.out.println(arr); // 測試removeLast()方法 arr.removeLast(); System.out.println('刪除數(shù)組中最后一個(gè)元素:'); System.out.println(arr); // 測試removeElement(int e)方法 arr.removeElement(6); System.out.println('刪除數(shù)組中指定元素:'); System.out.println(arr); // 測試contains(int e)方法 boolean isContains = arr.contains(1); System.out.println('數(shù)組中是否存在元素e:'); System.out.println('isContains = ' + isContains); // 測試find(int e)方法 int index = arr.find(2); System.out.println('元素e在數(shù)組中的索引:'); System.out.println('index = ' + index); }}

結(jié)果如下:

添加數(shù)組元素:

Array: size = 10 , capacity = 20

[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

[-10,0,200,1,2,3,4,5,6,7,8,9]

根據(jù)數(shù)組索引查找數(shù)組元素:

9

修改數(shù)組索引位置上元素值:

1000

Array: size = 12 , capacity = 20

[-10,0,200,1,2,3,4,5,6,7,8,1000]

刪除數(shù)組中指定index元素:

Array: size = 11 , capacity = 20

[0,200,1,2,3,4,5,6,7,8,1000]

刪除數(shù)組中第一個(gè)元素:

Array: size = 10 , capacity = 20

[200,1,2,3,4,5,6,7,8,1000]

刪除數(shù)組中最后一個(gè)元素:

Array: size = 9 , capacity = 20

[200,1,2,3,4,5,6,7,8]

刪除數(shù)組中指定元素:

Array: size = 8 , capacity = 20

[200,1,2,3,4,5,7,8]

數(shù)組中是否存在元素e:

isContains = true

元素e在數(shù)組中的索引:

index = 2

關(guān)于本小節(jié)只是簡單的對(duì)數(shù)組中的一個(gè)元素進(jìn)行操作,并進(jìn)行了簡單的測試。

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《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)文章:
主站蜘蛛池模板: 肇东市| 新疆| 石嘴山市| 且末县| 仪陇县| 奇台县| 丹棱县| 洛宁县| 吉林市| 嵊州市| 金山区| 顺义区| 沐川县| 杭锦旗| 安阳县| 蒲城县| 新乡县| 梁河县| 巨野县| 工布江达县| 谢通门县| 阿克苏市| 井冈山市| 镇远县| 长顺县| 广平县| 十堰市| 宣汉县| 延边| 中阳县| 桓仁| 云安县| 威信县| 台北市| 库尔勒市| 六盘水市| 崇阳县| 通州市| 安阳县| 积石山| 盐津县|