最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)順序表的操作詳解

 更新時(shí)間:2022年09月19日 11:29:01   作者:熬夜磕代碼丶  
順序表是用一段物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線(xiàn)性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。本文主要介紹了順序表的實(shí)現(xiàn)與常用操作,需要的可以參考一下

一、順序表是什么

順序表是用一段物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線(xiàn)性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。在數(shù)組上完成數(shù)據(jù)的增刪查改。

數(shù)組不就是一個(gè)現(xiàn)場(chǎng)的順序表嗎?但是數(shù)組并沒(méi)有直接向我們提供增刪查改的工具,所以我們必須重新實(shí)現(xiàn)一下順序表。

二、自定義異常

空引用異常

如果我們的順序表為空時(shí),手動(dòng)拋出空引用異常

public class NullException extends RuntimeException{
    public NullException(String message) {
        super(message);
    }
}

下標(biāo)越界異常

當(dāng)我們進(jìn)行增刪查改時(shí),下標(biāo)越界時(shí),我們手動(dòng)拋出一個(gè)下標(biāo)越界異常

public class IndexException extends RuntimeException{
    public IndexException(String message) {
        super(message);
    }
}

三、順序表的方法

順序表的實(shí)現(xiàn)

這里我們定義一個(gè)順序表,默認(rèn)容量為DEFAULTSIZE,實(shí)際大小為usedsize.

public class ArrList {
    public int[] arr;
    public int usedSize;
    public static final int DEFAULTSIZE = 10;

    public ArrList() {
        this.arr = new int[DEFAULTSIZE];
    }
}

獲取順序表長(zhǎng)度

usedSize存儲(chǔ)的就是當(dāng)前順序表的長(zhǎng)度,直接返回即可。

public int size() { 
        return this.usedSize;
    }

順序表是否為空

此方法我們只想在順序表內(nèi)部使用,所以我們定義為private.

private boolean isEmpty() {
        return this.arr == null;
    }

順序表是否為滿(mǎn)

此方法我們只想在順序表內(nèi)部使用,所以我們定義為private.

private boolean isFull() {
        //如果數(shù)組所放元素大于等于數(shù)組長(zhǎng)度,那么數(shù)組滿(mǎn)了
        return this.size() >= this.arr.length;
    }

打印順序表

public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }

末尾新增元素

public void add(int data) throws NullException{
        //1.數(shù)組為空,報(bào)空異常
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.數(shù)組滿(mǎn)了,先增容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //3.進(jìn)行新增
        this.arr[this.usedSize] = data;
        //4.元素+1
        this.usedSize++;
    }

指定位置新增元素

public void add(int pos, int data) throws RuntimeException,IndexException{
        //1.判斷數(shù)組是否為空
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.判斷新增位置是否合法,拋數(shù)組越界異常
        if(pos < 0 || pos > this.arr.length) {
            throw new IndexException("數(shù)組越界");
        }
        //3.判斷數(shù)組是否已滿(mǎn),進(jìn)行擴(kuò)容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //4.進(jìn)行新增
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.arr[i+1] = this.arr[i];
        }
        this.arr[pos] = data;
        this.usedSize++;
    }

判斷是否包含某元素

public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.arr[i]) {
                return true;
            }
        }
        return false;
    }

查找某個(gè)元素對(duì)應(yīng)的位置

public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.arr[i]) {
                return i;
            }
        }
        return -1; 
    }

獲取 pos 位置的元素

 public int get(int pos) throws IndexException{
        //判斷pos位置是否合法
        if(pos < 0 || pos >= this.usedSize) {
            throw new IndexException("輸入pos位置數(shù)組越界");
        }else {
            return this.arr[pos];
        }
    }

給 pos 位置的元素賦值

public void set(int pos, int value) throws NullException,IndexException{
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.判斷新增位置是否合法,拋數(shù)組越界異常
        if(pos < 0 || pos >= this.arr.length) {
            throw new IndexException("數(shù)組越界");
        }
        this.arr[pos] = value;
    }

刪除第一次出現(xiàn)的關(guān)鍵字key

public void remove(int toRemove) throws NullException{
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        int ret = indexOf(toRemove);
        if(ret == -1) {
            System.out.println("不存在此數(shù)");
            return;
        }
        if(ret != -1) {
            for (int i = ret; i < this.usedSize - 1; i++) {
                this.arr[i] = this.arr[i+1];
            }
        }
        this.usedSize++;
    }

清空順序表

   public void clear() {
        this.usedSize = 0;
        //如果為引用類(lèi)型
//        for (int i = 0; i < size(); i++) {
//            this.arr[i] = null;
//        }
//        this.usedSize = 0;
    }
}

四、自定義順序表

public class ArrList {
    public int[] arr;
    public int usedSize;
    public static final int DEFAULTSIZE = 10;

    public ArrList() {
        this.arr = new int[DEFAULTSIZE];
    }
    // 打印順序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
    // 新增元素,默認(rèn)在數(shù)組最后新增
    public void add(int data) throws NullException{
        //1.數(shù)組為空,報(bào)空異常
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.數(shù)組滿(mǎn)了,先增容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //3.進(jìn)行新增
        this.arr[this.usedSize] = data;
        //4.元素+1
        this.usedSize++;
    }
    private boolean isFull() {
        //如果數(shù)組所放元素大于等于數(shù)組長(zhǎng)度,那么數(shù)組滿(mǎn)了
        return this.size() >= this.arr.length;
    }
   private boolean isEmpty() {
        return this.arr == null;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) throws RuntimeException,IndexException{
        //1.判斷數(shù)組是否為空
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.判斷新增位置是否合法,拋數(shù)組越界異常
        if(pos < 0 || pos > this.arr.length) {
            throw new IndexException("數(shù)組越界");
        }
        //3.判斷數(shù)組是否已滿(mǎn),進(jìn)行擴(kuò)容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //4.進(jìn)行新增
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.arr[i+1] = this.arr[i];
        }
        this.arr[pos] = data;
        this.usedSize++;
    }
    // 判定是否包含某個(gè)元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.arr[i]) {
                return true;
            }
        }
        return false;
    }
    // 查找某個(gè)元素對(duì)應(yīng)的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.arr[i]) {
                return i;
            }
        }
        return -1;
    }
    // 獲取 pos 位置的元素
    public int get(int pos) throws IndexException{
        //判斷pos位置是否合法
        if(pos < 0 || pos >= this.usedSize) {
            throw new IndexException("輸入pos位置數(shù)組越界");
        }else {
            return this.arr[pos];
        }
    }
    // 給 pos 位置的元素設(shè)為 value
    public void set(int pos, int value) throws NullException,IndexException{
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.判斷新增位置是否合法,拋數(shù)組越界異常
        if(pos < 0 || pos >= this.arr.length) {
            throw new IndexException("數(shù)組越界");
        }
        this.arr[pos] = value;
    }
    //刪除第一次出現(xiàn)的關(guān)鍵字key
    public void remove(int toRemove) throws NullException{
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        int ret = indexOf(toRemove);
        if(ret == -1) {
            System.out.println("不存在此數(shù)");
            return;
        }
        if(ret != -1) {
            for (int i = ret; i < this.usedSize - 1; i++) {
                this.arr[i] = this.arr[i+1];
            }
        }
        this.usedSize++;
    }
    // 獲取順序表長(zhǎng)度
    public int size() {
        return this.usedSize;
    }
    // 清空順序表
    public void clear() {
        this.usedSize = 0;
        //如果為引用類(lèi)型
//        for (int i = 0; i < size(); i++) {
//            this.arr[i] = null;
//        }
//        this.usedSize = 0;
    }
}

以上就是Java實(shí)現(xiàn)順序表的操作詳解的詳細(xì)內(nèi)容,更多關(guān)于Java順序表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 你知道jdk竟有4個(gè)random嗎

    你知道jdk竟有4個(gè)random嗎

    這篇文章主要給大家介紹了關(guān)于jdk中4個(gè)random的相關(guān)資料,分別是Random、ThreadLocalRandom、SecureRandom以及SplittableRandom,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-06-06
  • java實(shí)現(xiàn)單機(jī)版五子棋

    java實(shí)現(xiàn)單機(jī)版五子棋

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)單機(jī)版五子棋源碼,以及五子棋游戲需要的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java8中對(duì)泛型目標(biāo)類(lèi)型推斷方法的改進(jìn)

    Java8中對(duì)泛型目標(biāo)類(lèi)型推斷方法的改進(jìn)

    這篇文章主要介紹了Java8中對(duì)泛型目標(biāo)類(lèi)型推斷方法的改進(jìn),需要的朋友可以參考下
    2014-06-06
  • 深入淺析HashMap key和value能否為null

    深入淺析HashMap key和value能否為null

    HashMap的key和value可為null,線(xiàn)程不安全,HashTable的key和value均不可為null,線(xiàn)程安全,ConcurrentHashMap在多線(xiàn)程場(chǎng)景下使用,key和value也不能為null,還對(duì)它們進(jìn)行了測(cè)試和底層代碼分析,本文介紹HashMap key和value能否為null,感興趣的朋友跟隨小編一起看看吧
    2025-04-04
  • 如何使用nexus在局域網(wǎng)內(nèi)搭建maven私服及idea的使用

    如何使用nexus在局域網(wǎng)內(nèi)搭建maven私服及idea的使用

    這篇文章主要介紹了如何使用nexus在局域網(wǎng)內(nèi)搭建maven私服及idea的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • SpringMVC攔截器詳解

    SpringMVC攔截器詳解

    本篇文章主要介紹了SpringMVC攔截器配置及使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-07-07
  • Java Swing中的表格(JTable)和樹(shù)(JTree)組件使用實(shí)例

    Java Swing中的表格(JTable)和樹(shù)(JTree)組件使用實(shí)例

    這篇文章主要介紹了Java Swing中的表格(JTable)和樹(shù)(JTree)組件使用實(shí)例,本文同時(shí)講解了表格和樹(shù)的基本概念、常用方法、代碼實(shí)例,需要的朋友可以參考下
    2014-10-10
  • 淺談JackSon的幾種用法

    淺談JackSon的幾種用法

    這篇文章主要介紹了淺談JackSon的幾種用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • JAVA加密算法數(shù)字簽名實(shí)現(xiàn)原理詳解

    JAVA加密算法數(shù)字簽名實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了JAVA加密算法數(shù)字簽名實(shí)現(xiàn)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • springMVC實(shí)現(xiàn)文件上傳和下載

    springMVC實(shí)現(xiàn)文件上傳和下載

    這篇文章主要為大家詳細(xì)介紹了springMVC實(shí)現(xiàn)文件上傳和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論

达孜县| 石泉县| 达拉特旗| 西安市| 建昌县| 雷山县| 武功县| 泰顺县| 灯塔市| 当阳市| 且末县| 新宁县| 廊坊市| 台南县| 曲阜市| 乌拉特中旗| 庆阳市| 瑞安市| 安义县| 仁怀市| 西乌珠穆沁旗| 格尔木市| 涡阳县| 高青县| 平远县| 安阳县| 称多县| 清原| 西乌珠穆沁旗| 德州市| 泾川县| 清丰县| 娱乐| 宜宾市| 濮阳县| 长汀县| 天台县| 大石桥市| 巴中市| 睢宁县| 名山县|