溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)

發(fā)布時間:2022-08-23 11:45:50 來源:億速云 閱讀:147 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)”文章能幫助大家解決問題。

    前言

    線性表(linear list)是n個具有相同特性的數(shù)據(jù)元素的有限序列。 線性表是一種在實際中廣泛使用的數(shù)據(jù)結(jié)構(gòu),常見 的線性表:順序表、鏈表、棧、隊列、字符串… 線性表在邏輯上是線性結(jié)構(gòu),也就說是連續(xù)的一條直線。但是在物理結(jié)構(gòu)上并不一定是連續(xù)的,線性表在物理上存儲 時,通常以數(shù)組和鏈式結(jié)構(gòu)的形式存儲。

    Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)

    一、順序表

    1.1 什么是順序表

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

    其實就是一個數(shù)組。那為什么還要寫一個順序表,直接用數(shù)組不就好了?不一樣的,寫到類里面就可以面向?qū)ο蟆?/p>

    順序表一般可以分為:

    • 靜態(tài)順序表:使用定長數(shù)組存儲

    • 動態(tài)順序表:使用動態(tài)開辟的數(shù)組存儲

    靜態(tài)順序表適用于確定知道需要存多少數(shù)據(jù)的場景.

    靜態(tài)順序表的定長數(shù)組導(dǎo)致N定大了,空間開多了浪費,開少了不夠用.

    相比之下動態(tài)順序表更靈活, 根據(jù)需要動態(tài)的分配空間大小.

    二、簡單實現(xiàn)順序表

    2.1 創(chuàng)建順序表

    public class MyArrayList {
       public int[] elem;//數(shù)組
       public int usedSize;//數(shù)據(jù)的有效個數(shù)
     
       public MyArrayList(){
           this.elem = new int[10];
       }
    }

    Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)

    2.2 打印順序表

    //打印順序表
    public void display(){
            for (int i = 0; i < this.usedSize; i++) {
                System.out.print(this.elem[i] + " ");
            }
            System.out.println();
        }

    2.3 獲取順序表長度

    //獲取順序表長度
        public int size(){
            return this.usedSize;
       }

    2.4 在 pos 位置新增元素

    在順序表里面插入元素的時候所插入的位置的前面一定是存放了元素的

    Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)

    //在 pos 位置新填元素
        public void add(int pos,int data){
            if(pos < 0 || pos >usedSize){
                System.out.println("pos 位置不合法!");
                return;
            }
            if(isfull()) {
                Arrays.copyOf(this.elem,2*this.elem.length);
            }
            for (int i = this.usedSize - 1; i >= pos; i--) {
                this.elem[i + 1] = this.elem[i];
            }
            this.elem[pos] = data;
            this.usedSize++;
        }
        //判斷是否滿
        public boolean isfull(){
            return this.usedSize == this.elem.length;
        }

    2.5 判定是否包含某個元素

    //判斷是否包含某個元素
    public boolean contains(int toFind){
            for (int i = 0; i < this.usedSize; i++) {
                if(this.elem[i] == toFind){
                    return true;
                }
            }
            return false;
        }

    2.6 查找某個元素對應(yīng)的位置

    //查找某個元素的對應(yīng)位置,找不到返回-1
        public int search(int toFind){
            for (int i = 0; i < this.usedSize; i++) {
                if(this.elem[i] == toFind){
                    return i;
                }
            }
            return -1;
        }

    2.7 獲取 pos 位置的元素

    //獲取pos位置的值
        public int getPos(int pos){
            if(pos < 0 || pos >= this.usedSize){
                System.out.println("pos 位置不合法");
                return -1;//這里說明一下,業(yè)務(wù)上的處理,不考慮
            }
            if(isEmpty()){
                System.out.println("順序表為空!");
                return -1;
            }
            return this.elem[pos];
        }
        public boolean isEmpty(){
            return this.usedSize == 0;
        }

    2.8 給 pos 位置的元素設(shè)為 value

     //給pos位置元素更新value
        public void setPos(int pos,int value){
            if (pos < 0 || pos >= this.usedSize){
                System.out.println("pos 位置不合法");
                return;
            }
            if(isEmpty()){
                System.out.println("順序表為空!");
                return;
            }
            this.elem[pos] = value;
        }

    2.9 刪除你想要刪除的元素

    //刪除第一次出現(xiàn)的關(guān)鍵字key
        public void remove(int toRmove){
            if (isEmpty()){
                System.out.println("順序表為空!");
                return;
            }
            int index = search(toRmove);
            if(index == -1){
                System.out.println("沒有你要刪除的數(shù)字!");
                return;
            }
            for (int i = index; i < this.usedSize - 1; i++) {
                this.elem[i] = this.elem[i+1];
            }
            this.usedSize--;
            //this.elem[useSize] = null;如果數(shù)組當(dāng)中是引用數(shù)據(jù)類型
        }

    2.10 清空順序表

    //清空順序表
        public void clear(){
            this.usedSize = 0;
        }

    三、MyArrayList.java

    import java.util.Arrays;
    
    public class MyArrayList {
    
        public int[] elem;
        public int usedSize;
    
        public MyArrayList(){
            this.elem = new int[10];
        }
        //打印順序表
        public void display(){
            for (int i = 0; i < this.usedSize; i++) {
                System.out.print(this.elem[i] + " ");
            }
            System.out.println();
        }
        //獲取順序表長度
        public int size(){
            return this.usedSize;
        }
        //在 pos 位置新填元素
        public void add(int pos,int data){
            if(pos < 0 || pos >usedSize){
                System.out.println("pos 位置不合法!");
                return;
            }
            if(isfull()) {
                Arrays.copyOf(this.elem,2*this.elem.length);
            }
            for (int i = this.usedSize - 1; i >= pos; i--) {
                this.elem[i + 1] = this.elem[i];
            }
            this.elem[pos] = data;
            this.usedSize++;
        }
        //判斷是否滿
        public boolean isfull(){
            return this.usedSize == this.elem.length;
        }
    
        //判斷是否包含某個元素
        public boolean contains(int toFind){
            for (int i = 0; i < this.usedSize; i++) {
                if(this.elem[i] == toFind){
                    return true;
                }
            }
            return false;
        }
        //查找某個元素的對應(yīng)位置,找不到返回-1
        public int search(int toFind){
            for (int i = 0; i < this.usedSize; i++) {
                if(this.elem[i] == toFind){
                    return i;
                }
            }
            return -1;
        }
    
        //獲取pos位置的值
        public int getPos(int pos){
            if(pos < 0 || pos >= this.usedSize){
                System.out.println("pos 位置不合法");
                return -1;//這里說明一下,業(yè)務(wù)上的處理,不考慮
            }
            if(isEmpty()){
                System.out.println("順序表為空!");
                return -1;
            }
            return this.elem[pos];
        }
        public boolean isEmpty(){
            return this.usedSize == 0;
        }
        //給pos位置元素更新value
        public void setPos(int pos,int value){
            if (pos < 0 || pos >= this.usedSize){
                System.out.println("pos 位置不合法");
                return;
            }
            if(isEmpty()){
                System.out.println("順序表為空!");
                return;
            }
            this.elem[pos] = value;
        }
    
        //刪除第一次出現(xiàn)的關(guān)鍵字key
        public void remove(int toRmove){
            if (isEmpty()){
                System.out.println("順序表為空!");
                return;
            }
            int index = search(toRmove);
            if(index == -1){
                System.out.println("沒有你要刪除的數(shù)字!");
                return;
            }
            for (int i = index; i < this.usedSize - 1; i++) {
                this.elem[i] = this.elem[i+1];
            }
            this.usedSize--;
            //this.elem[useSize] = null;如果數(shù)組當(dāng)中是引用數(shù)據(jù)類型
        }
        //清空順序表
        public void clear(){
            this.usedSize = 0;
        }
    }

    四、Test.java

    public class Test {
        public static void main(String[] args) {
            MyArrayList myArrayList = new MyArrayList();
            myArrayList.add(0,1);
            myArrayList.add(1,2);
            myArrayList.add(2,3);
            myArrayList.add(3,4);
            myArrayList.add(4,5);
            myArrayList.display();
            System.out.println(myArrayList.contains(3));
            System.out.println(myArrayList.getPos(3));
            myArrayList.setPos(0,99);
            myArrayList.display();
        }
    }

    關(guān)于“Java數(shù)據(jù)結(jié)構(gòu)之順序表如何實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI