欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、順序表是什么

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

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

二、自定義異常

空引用異常

如果我們的順序表為空時,手動拋出空引用異常

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

下標越界異常

當(dāng)我們進行增刪查改時,下標越界時,我們手動拋出一個下標越界異常

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

三、順序表的方法

順序表的實現(xiàn)

這里我們定義一個順序表,默認容量為DEFAULTSIZE,實際大小為usedsize.

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

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

獲取順序表長度

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

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

順序表是否為空

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

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

順序表是否為滿

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

private boolean isFull() {
        //如果數(shù)組所放元素大于等于數(shù)組長度,那么數(shù)組滿了
        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ù)組為空,報空異常
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.數(shù)組滿了,先增容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //3.進行新增
        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ù)組是否已滿,進行擴容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //4.進行新增
        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;
    }

查找某個元素對應(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;
        //如果為引用類型
//        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();
    }
    // 新增元素,默認在數(shù)組最后新增
    public void add(int data) throws NullException{
        //1.數(shù)組為空,報空異常
        if(isEmpty()) {
            throw new NullException("數(shù)組為空");
        }
        //2.數(shù)組滿了,先增容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //3.進行新增
        this.arr[this.usedSize] = data;
        //4.元素+1
        this.usedSize++;
    }
    private boolean isFull() {
        //如果數(shù)組所放元素大于等于數(shù)組長度,那么數(shù)組滿了
        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ù)組是否已滿,進行擴容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //4.進行新增
        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;
    }
    // 查找某個元素對應(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++;
    }
    // 獲取順序表長度
    public int size() {
        return this.usedSize;
    }
    // 清空順序表
    public void clear() {
        this.usedSize = 0;
        //如果為引用類型
//        for (int i = 0; i < size(); i++) {
//            this.arr[i] = null;
//        }
//        this.usedSize = 0;
    }
}

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

相關(guān)文章

  • springboot使用之多個filter的執(zhí)行順序以及配置方式

    springboot使用之多個filter的執(zhí)行順序以及配置方式

    這篇文章主要介紹了springboot使用之多個filter的執(zhí)行順序以及配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach

    MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach

    這篇文章主要介紹了MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • JAVA中取整數(shù)的4種方法總結(jié)

    JAVA中取整數(shù)的4種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于JAVA中取整數(shù)的4種方法,在java的Math類中,提供了許許多多的和數(shù)學(xué)計算有關(guān)的方法,其中也包括取整的,需要的朋友可以參考下
    2023-07-07
  • Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法

    Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法

    本篇文章主要介紹了Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法,具有一定的參考價值,有興趣的可以了解一下。
    2017-04-04
  • SpringBoot任務(wù)之定時任務(wù)相關(guān)知識總結(jié)

    SpringBoot任務(wù)之定時任務(wù)相關(guān)知識總結(jié)

    今天給大家整理的文章是SpringBoot定時任務(wù)的相關(guān)知識點,文中有非常詳細的介紹及代碼示例,對正在學(xué)習(xí)SpringBoot任務(wù)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • java正則表達式的簡單運用

    java正則表達式的簡單運用

    這篇文章主要為大家詳細介紹了java正則表達式的簡單運用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • java中的FileInputStream(輸入流)

    java中的FileInputStream(輸入流)

    這篇文章主要介紹了java中的FileInputStream(輸入流),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • java 發(fā)送帶Basic Auth認證的http post請求實例代碼

    java 發(fā)送帶Basic Auth認證的http post請求實例代碼

    下面小編就為大家?guī)硪黄猨ava 發(fā)送帶Basic Auth認證的http post請求實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • Mybatis環(huán)境配置及測試詳解

    Mybatis環(huán)境配置及測試詳解

    這篇文章主要介紹了Mybatis環(huán)境配置及測試詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java中super關(guān)鍵字的用法和細節(jié)

    Java中super關(guān)鍵字的用法和細節(jié)

    大家好,本篇文章主要講的是Java中super關(guān)鍵字的用法和細節(jié),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01

最新評論