欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

在宣漢等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都營銷網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè)公司,宣漢網(wǎng)站建設(shè)費(fèi)用合理。

前言:在上一小節(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除的索引對應(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除的索引對應(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ù)組中刪除元素我們并不需要返回被刪除的元素,這是由于對于使用者來說,已經(jīng)知道自己要?jiǎng)h除的值是多少了,內(nèi)部無須在返回,

(2)針對通過索引方式刪除的元素需要返回被刪除,這是由于用戶并不知道自己刪除的元素值是什么,我們把被刪除的值返回給用戶,以便于用戶在需要使用時(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é)只是簡單的對數(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é)》

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

網(wǎng)頁題目:Java封裝數(shù)組實(shí)現(xiàn)包含、搜索和刪除元素操作詳解
網(wǎng)址分享:http://www.chinadenli.net/article18/ishdgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站企業(yè)網(wǎng)站制作建站公司域名注冊自適應(yīng)網(wǎng)站動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)