国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

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

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

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

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í)候在查詢(xún)過(guò)程中,我們不僅想知道是否包含該指定元素,還想是在該元素所在的位置,則我們可以編寫(xiě)一個(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位置的元素的方法,接下來(lái),我們可以封裝一些其他的方法:

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

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

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

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

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

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

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

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

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

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

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

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

更多關(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)文章:
主站蜘蛛池模板: 水城县| 无极县| 青岛市| 广宁县| 承德市| 迁西县| 社会| 镇坪县| 龙江县| 阜城县| 法库县| 麦盖提县| 东阿县| 揭阳市| 陈巴尔虎旗| 南投县| 汝城县| 昌平区| 德惠市| 虹口区| 江西省| 柞水县| 铁岭市| 平定县| 灵台县| 闻喜县| 绥德县| 滦南县| 临湘市| 怀柔区| 祁东县| 偏关县| 无为县| 南涧| 汨罗市| 揭西县| 东兴市| 景洪市| 班玛县| 和平区| 开远市|