最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)線性表的順序存儲(chǔ)

 更新時(shí)間:2020年10月29日 13:34:11   作者:I like study.  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)線性表的順序存儲(chǔ),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)線性表的順序存儲(chǔ),供大家參考,具體內(nèi)容如下

順序表:用一組地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)各個(gè)元素,使得在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲(chǔ)在相鄰的物理存儲(chǔ)單元中的線性表

package algorithm.datastructure.seqlist;

/*順序表
*
* 用一組地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)各個(gè)元素,使得在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲(chǔ)在相鄰的物理存儲(chǔ)單元中的線性表
*
*/
public class SeqList {

  private int length;//順序表長(zhǎng)度
  private int[] list;//數(shù)組,連續(xù)的存儲(chǔ)空間

  //初始化,構(gòu)造一個(gè)空的線性表
  public SeqList(int listLength) {
    list = new int[listLength];
  }
  //銷毀表
  public void destroyList() {
    list = null;
    this.length = 0;
  }
  //將線性表置為空表
  public void clearList() {
    for (int i = 0; i < getLength(); i++) {
      list[i] = 0;
    }
  }

  //判斷線性表是否未空表
  public Boolean isEmpty() {
    return getLength() == 0;
  }

  //獲取線性表元素個(gè)數(shù)
  public int getLength() {
    return length;
  }

  //根據(jù)下標(biāo)獲取線性表元素
  public int getElem(int i) {
    if (i < 0 || i >= getLength()) {
      try {
        throw new Exception("線性表下標(biāo)越界");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return list[i];
  }


  //返回某元素(第一個(gè))的前驅(qū)
  public Integer priorElem(int element) {
    for (int i = 0; i < getLength(); i++) {
      if (element == list[i]) {
        if (i == 0) {
          return null;
        } else {
          return list[i - 1];
        }
      }
    }
    return null;
  }

  //獲取某元素(第一個(gè))的后繼
  public Integer nextElem(int element) {
    for (int i = 0; i < getLength(); i++) {
      if (element == list[i]) {
        if (i == getLength() - 1) {
          return null;
        } else {
          return list[i + 1];
        }
      }
    }
    return null;
  }

  //擴(kuò)容,這里設(shè)置容量變?yōu)樵瓉?lái)兩倍
  public void ensureCapacity(int capacity) {
    if (capacity >= list.length) {//擴(kuò)容
      int tempList[] = new int[list.length * 2];
      for (int i = 0; i < list.length; i++) {
        tempList[i] = list[i];
      }
      list = tempList;
    }
  }

  //在指定位置插入元素
  public Boolean insertElement(int index, int element) {
    if (index < 0 || index >= list.length) {
      try {
        throw new Exception("下標(biāo)錯(cuò)誤");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (index == getLength()) {
      return insertTailElement(element);
    }
    for (int i = 0; i < getLength(); i++) {
      if (i == index) {
        ensureCapacity(getLength() + 1);
        //index位置后面的元素后移
        for (int j = getLength() - 1; j >= index; j--) {
          list[j + 1] = list[j];
        }
        list[index] = element;
        length++;
      }
    }
    return true;
  }

  //尾部插入元素
  public Boolean insertTailElement(int element) {
    ensureCapacity(length + 1);
    list[++length] = element;
    return true;
  }
  //刪除尾部元素
  public int deleteTailElement() {
    if (getLength() == 0) {
      try {
        throw new Exception("下標(biāo)錯(cuò)誤");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    int tailElement = list[getLength() - 1];
    list[getLength() - 1] = 0;
    length--;
    return tailElement;
  }

  //刪除元素
  public int deleteElement(int index) {
    if (index < 0 || index >= list.length) {
      try {
        throw new Exception("下標(biāo)錯(cuò)誤");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (index == getLength()) {
      return deleteTailElement();
    }
    for (int i = 0; i < getLength(); i++) {
      if (i == index) {
        int tailElement = list[index];
        //index位置后面的元素前移
        for (int j = index; j < getLength() - 1; j++) {
          list[j] = list[j + 1];
        }
        list[getLength() - 1] = 0;
        length--;
        return tailElement;
      }
    }
    return 0;
  }

  //遍歷順序表
  public void traverseList() {
    for (int i = 0; i < getLength(); i++) {
      System.out.println(list[i]);
    }
  }



  public static void main(String[] args) {
    //測(cè)試
    SeqList seqList = new SeqList(2);
    System.out.println(seqList.insertTailElement(1));
    System.out.println(seqList.insertTailElement(2));
    System.out.println(seqList.insertTailElement(3));
    System.out.println(seqList.insertTailElement(4));
    System.out.println(seqList.getElem(0));
    System.out.println(seqList.getElem(1));
    System.out.println(seqList.getElem(2));
    System.out.println(seqList.getElem(3));

    System.out.println(seqList.insertElement(0, 4));
    System.out.println(seqList.getElem(0));
    System.out.println(seqList.getElem(1));
    System.out.println(seqList.getElem(2));
    System.out.println(seqList.getElem(3));
    System.out.println(seqList.getElem(4));
    System.out.println(seqList.priorElem(3));
    System.out.println(seqList.priorElem(4));
    System.out.println(seqList.nextElem(4));
    System.out.println(seqList.nextElem(3));
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
    System.out.println(seqList.deleteElement(0));
    System.out.println(seqList.deleteElement(1));
    seqList.traverseList();
  }
}

以上就是用Java簡(jiǎn)單實(shí)現(xiàn)的順序表,在Java中,如果要實(shí)現(xiàn)功能更復(fù)雜,性能更高的順序表,可參考ArrayList源碼。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot+mybaties項(xiàng)目中掃描不到@mapper注解的解決方法

    springboot+mybaties項(xiàng)目中掃描不到@mapper注解的解決方法

    本文主要介紹了springboot+mybaties項(xiàng)目中掃描不到@mapper注解的解決方法,該報(bào)錯(cuò)表明掃描不到Mapper層,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • spring security CSRF防護(hù)的示例代碼

    spring security CSRF防護(hù)的示例代碼

    這篇文章主要介紹了spring security CSRF防護(hù)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • IDEA關(guān)閉git管理,文件變成紅色解決方案

    IDEA關(guān)閉git管理,文件變成紅色解決方案

    在軟件開發(fā)中,當(dāng)一個(gè)文件夾內(nèi)的Java項(xiàng)目啟用Git版本控制,通常會(huì)導(dǎo)致該文件夾下所有項(xiàng)目同步開啟Git,這種做法有助于保持項(xiàng)目的一致性和可追溯性,但也可能帶來(lái)管理上的復(fù)雜性,如果需要解除某個(gè)項(xiàng)目的Git管理,可以通過(guò)IDE的設(shè)置選項(xiàng)進(jìn)行調(diào)整
    2024-10-10
  • springboot集成mybatisplus的詳細(xì)步驟

    springboot集成mybatisplus的詳細(xì)步驟

    MyBatis-Plus (opens new window)(簡(jiǎn)稱 MP)是一個(gè) MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,這篇文章主要介紹了springboot四步集成mybatisplus,需要的朋友可以參考下
    2022-10-10
  • 劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串

    劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • 使用Java讀取Excel文件數(shù)據(jù)的方法詳解

    使用Java讀取Excel文件數(shù)據(jù)的方法詳解

    通過(guò)編程方式讀取Excel數(shù)據(jù)能實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入、批量處理、數(shù)據(jù)比對(duì)和更新等任務(wù)的自動(dòng)化,本文為大家介紹了三種Java讀取Excel文件數(shù)據(jù)的方法,需要的可以參考下
    2024-01-01
  • mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn)

    mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn)

    本文主要介紹了mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Java創(chuàng)建對(duì)象的四種方式詳解

    Java創(chuàng)建對(duì)象的四種方式詳解

    這篇文章主要介紹了Java創(chuàng)建對(duì)象的四種方式詳解,如果我們不想利用默認(rèn)構(gòu)造器來(lái)創(chuàng)建java對(duì)象,而想利用指定的構(gòu)造器來(lái)創(chuàng)建java對(duì)象,則需要利用Construtor對(duì)象,每個(gè)Construtor對(duì)應(yīng)一個(gè)構(gòu)造器,需要的朋友可以參考下
    2023-11-11
  • SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼

    SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼

    在實(shí)際開發(fā)中,肯定會(huì)經(jīng)常遇到對(duì)參數(shù)字段進(jìn)行校驗(yàn)的場(chǎng)景,通常我們只能寫大量的if else來(lái)完成校驗(yàn)工作,而如果使用SpringBoot Validation則可以輕松的通過(guò)注解來(lái)完成,接下來(lái)小編給大家介紹下利用SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼,需要的朋友參考下吧
    2022-06-06
  • IntelliJ IDEA 2020.3 重大特性(新功能一覽)

    IntelliJ IDEA 2020.3 重大特性(新功能一覽)

    這篇文章主要介紹了IntelliJ IDEA 2020.3 重大特性(新功能一覽),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論

阜南县| 巫山县| 清水县| 馆陶县| 大连市| 龙岩市| 都安| 荥阳市| 湖南省| 南溪县| 宜兰县| 桐柏县| 曲麻莱县| 卢氏县| 彩票| 宜章县| 南皮县| 上高县| 沭阳县| 桂东县| 乌兰县| 南郑县| 松桃| 天镇县| 云和县| 武邑县| 鸡泽县| 蒙城县| 靖宇县| 成武县| 永善县| 平南县| 阜阳市| 仙居县| 资兴市| 泸定县| 和平区| 阳朔县| 章丘市| 墨江| 宁都县|