欧美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);
    }
}

下標(biāo)越界異常

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

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

三、順序表的方法

順序表的實現(xiàn)

這里我們定義一個順序表,默認(rè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.進(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ù)組是否已滿,進(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;
    }

查找某個元素對應(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();
    }
    // 新增元素,默認(rèn)在數(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.進(jìn)行新增
        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ù)組是否已滿,進(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;
    }
    // 查找某個元素對應(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)順序表的操作詳解的詳細(xì)內(nèi)容,更多關(guān)于Java順序表的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論