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

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

 更新時(shí)間:2022年08月23日 10:49:50   作者:摸魚王胖嘟嘟  
線性表(linear?list)是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列。順序表是常見的線性表之一,本文將詳細(xì)講講順序表的原理與實(shí)現(xiàn),需要的可以參考一下

前言

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

一、順序表

1.1 什么是順序表

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

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

順序表一般可以分為:

  • 靜態(tài)順序表:使用定長(zhǎng)數(shù)組存儲(chǔ)
  • 動(dòng)態(tài)順序表:使用動(dòng)態(tài)開辟的數(shù)組存儲(chǔ)

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

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

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

二、簡(jiǎn)單實(shí)現(xiàn)順序表

2.1 創(chuàng)建順序表

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

2.2 打印順序表

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

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

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

2.4 在 pos 位置新增元素

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

//在 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 判定是否包含某個(gè)元素

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

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

//查找某個(gè)元素的對(duì)應(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 刪除你想要?jiǎng)h除的元素

//刪除第一次出現(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("沒有你要?jiǎng)h除的數(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();
    }
    //獲取順序表長(zhǎng)度
    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;
    }

    //判斷是否包含某個(gè)元素
    public boolean contains(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }
    //查找某個(gè)元素的對(duì)應(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("沒有你要?jiǎng)h除的數(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();
    }
}

以上就是Java數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Java順序表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java客戶端通過HTTPS連接到Easysearch實(shí)現(xiàn)過程

    Java客戶端通過HTTPS連接到Easysearch實(shí)現(xiàn)過程

    這篇文章主要為大家介紹了Java客戶端通過HTTPS連接到Easysearch實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Spring Security 和Apache Shiro你需要具備哪些條件

    Spring Security 和Apache Shiro你需要具備哪些條件

    這篇文章主要介紹了Spring Security 和Apache Shiro你需要具備哪些條件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java 數(shù)組轉(zhuǎn)List的四種方式小結(jié)

    Java 數(shù)組轉(zhuǎn)List的四種方式小結(jié)

    最近看了下數(shù)組轉(zhuǎn)List的實(shí)現(xiàn)方法,總共有4種,本文就詳細(xì)的介紹一下,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 淺談java是如何做資源回收補(bǔ)救的

    淺談java是如何做資源回收補(bǔ)救的

    這篇文章主要介紹了淺談java是如何做資源回收補(bǔ)救的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java正則表達(dá)式的替換和分組功能

    Java正則表達(dá)式的替換和分組功能

    這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式的替換和分組功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java連接mysql數(shù)據(jù)庫及測(cè)試是否連接成功的方法

    java連接mysql數(shù)據(jù)庫及測(cè)試是否連接成功的方法

    這篇文章主要介紹了java連接mysql數(shù)據(jù)庫及測(cè)試是否連接成功的方法,結(jié)合完整實(shí)例形式分析了java基于jdbc連接mysql數(shù)據(jù)庫并返回連接狀態(tài)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • Java實(shí)現(xiàn)讀取和寫入properties文件

    Java實(shí)現(xiàn)讀取和寫入properties文件

    這篇文章主要介紹了Java實(shí)現(xiàn)讀取和寫入properties文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java如何使用流去除集合中某個(gè)字段為空的對(duì)象

    Java如何使用流去除集合中某個(gè)字段為空的對(duì)象

    這篇文章主要給大家介紹了關(guān)于Java如何使用流去除集合中某個(gè)字段為空的對(duì)象,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Selenium+Tesseract-OCR智能識(shí)別驗(yàn)證碼爬取網(wǎng)頁數(shù)據(jù)的實(shí)例

    Selenium+Tesseract-OCR智能識(shí)別驗(yàn)證碼爬取網(wǎng)頁數(shù)據(jù)的實(shí)例

    本文主要介紹了Selenium+Tesseract-OCR智能識(shí)別驗(yàn)證碼爬取網(wǎng)頁數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 10分鐘在服務(wù)器部署好Jenkins的詳細(xì)過程

    10分鐘在服務(wù)器部署好Jenkins的詳細(xì)過程

    這篇文章主要介紹了10分鐘在服務(wù)器部署好Jenkins,本文主要是?Jenkins?的安裝部署,那前提我們應(yīng)該裝好?Git?Maven?JDK,準(zhǔn)備工作本文不給大家詳細(xì)介紹了,對(duì)服務(wù)器部署Jenkins相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-08-08

最新評(píng)論