App下載

如何用Java實(shí)現(xiàn)順序表

猿友 2020-12-31 15:03:43 瀏覽數(shù) (2897)
反饋

一、什么是順序表?

順序表就是按照順序存儲(chǔ)方式存儲(chǔ)的線性表,該線性表的結(jié)點(diǎn)按照邏輯次序依次存放在計(jì)算機(jī)的一組連續(xù)的存儲(chǔ)單元中。

由于順序表是依次存放的,只要知道了該順序表的首地址及每個(gè)數(shù)據(jù)元素所占用的存儲(chǔ)長(zhǎng)度,那么就很容易計(jì)算出任何一個(gè)數(shù)據(jù)元素(即數(shù)據(jù)結(jié)點(diǎn))的位置。

二、順序表的常見操作:

1、創(chuàng)建類和構(gòu)造方法

public class MyArrayList {

    private int [] elem;

    private int usedSize;

    public MyArrayList(){

        this.elem = new int [10];

    }

    public MyArrayList(int capacity){

        this.elem = new int[capacity];

    }

 }


2、擴(kuò)容

    ?public void resize(){

        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);

    }


3、判斷順序表是否為滿

      ?public boolean isFull(){

        if(this.usedSize == this.elem.length){

            return true;

        }

        return false;

    }


4、打印順序表

    ?public void display() {

        for (int i = 0;i < usedSize; i++) {

            System.out.print(elem[i]+"  ");

        }

        System.out.println();

    }


5、在 pos 位置新增元素

    ?public void add(int pos, int data) {

        if(isFull()){

            System.out.println("鏈表已滿!");

            resize();

        }

        if(pos < 0 || pos > this.usedSize){

            System.out.println("插入位置不合法!");

            return;

        }

        for (int i =  usedSize-1; i >= pos;i--) {

            elem[i+1] = elem[i];

        }

        elem[pos] = data;

        this.usedSize++;

    }


6、判斷是否包含某個(gè)元素

    ?public boolean contains(int toFind) {

        for(int i = 0; i < usedSize;i++){

            if(elem[i] == toFind){

                return true;

            }

        }

        return false;

    }


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

    public int search(int toFind) {       

        for(int i = 0; i < usedSize;i++){

            if(elem[i] == toFind){

                return i;

            }

        }

        return -1;

    }


8、獲取 pos 位置的元素

    public int getPos(int pos) {   

        if(pos < 0 || pos >= usedSize){

            System.out.println("該pos位置不合法!");

            return -1;

        }

        return elem[pos];

    }


9、給 pos 位置的元素修改為 value

    public void setPos(int pos, int value) {    

        if(pos < 0 || pos >= usedSize){

            System.out.println("該pos位置不合法!");

            return;

        }

        elem[pos] = value;

    }


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

    public void remove(int toRemove) {    

        int index = -1;

        for(int i = 0; i < this.usedSize;i++){

            if(this.elem[i] == toRemove){

                index = i;

            }

        }

        if(index == -1){

            System.out.println("未找到該元素!");

            return;

        }

        for(int j = index;j < this.usedSize-1;j++){

            this.elem[j] = this.elem[j+1];

        }

        this.usedSize--;

    }


11、獲取鏈表長(zhǎng)度

    public int size() {    

        return this.usedSize;

    }


12、清空順序表

      ?public void clear() {

        this.usedSize = 0;

    }

0 人點(diǎn)贊