Java實(shí)現(xiàn)一個(gè)順序表的完整代碼
實(shí)現(xiàn)一個(gè)順序表
接口實(shí)現(xiàn)
定義一個(gè)MyArrayList類,在類中實(shí)現(xiàn)以下函數(shù)
public class MyArrayList { }
數(shù)組的定義
public int[] elem;//定義一個(gè)整形數(shù)組 public int usize;//usize表示數(shù)組的長(zhǎng)度 public MyArrayList(){ this.elem = new int[5]; }
打印順序表
for循環(huán)打印順序表的每一位
public void display(){ for (int i = 0; i < this.usize; i++) { System.out.print(this.elem[i]+" "); } System.out.println(); }
在pos位置新增元素
先定義一個(gè)isFull函數(shù)判斷順序表是否滿了,滿了返回true,沒滿則返回false
public boolean isFull(){ if (this.usize == this.elem.length){ return true; } return false; }
將pos位置后的元素后移,順序表順序表長(zhǎng)度增加一位
public void add(int pos, int data){ //判斷順序表是否滿了 if (isFull()){ System.out.println("順序表已滿"); //擴(kuò)容 this.elem = Arrays.copyOf(this.elem,2*this.usize); } //判斷pos的合法性 if (pos < 0 || pos > this.usize){ System.out.println("pos位置不合法"); return; } //將pos位置后的數(shù)字后移 for (int i = this.usize-1; i >= pos; i--) { this.elem[i+1] = this.elem[i]; } this.elem[pos] = data; this.usize++; }
判定是否包含某個(gè)元素
public boolean contains(int key){ for (int i = 0; i < this.usize; i++) { if (this.elem[i] == key){ return true; } } return false; }
查找某個(gè)對(duì)應(yīng)元素的位置
返回它的位置
public int search(int key){ for (int i = 0; i < this.usize; i++) { if (this.elem[i] == key){ return i; } } return -1; }
獲取pos位置的元素
定義一個(gè)isEmpty函數(shù)判斷順序表是否為空
public boolean isEmpty(){ return this.usize == 0; }
public int getPos(int pos){ //判斷順序表是否為空 if (isEmpty()){ return -1; } //判斷pos 位置是否合法 if (pos < 0 || pos >= this.usize){ return -1; } return this.elem[pos]; }
給pos位置的元素設(shè)為value 更新為新的數(shù)字
public void setPos(int pos,int value){ //判斷順序表是否為空 if (isEmpty()){ return; } //判斷pos位置是否合法 if (pos < 0 || pos >= this.usize){ return; } this.elem[pos] = value; }
刪除第一次出現(xiàn)的關(guān)鍵字key
查找到關(guān)鍵字,從關(guān)鍵字所在的位置開始到順序表結(jié)束每一項(xiàng)前移,覆蓋掉關(guān)鍵字,長(zhǎng)度減少一位
public void remove(int key){ int index= search(key); if (key == -1){ System.out.println("關(guān)鍵字不存在"); return; } for (int i = key; i < this.usize-1; i++) { this.elem[i] = this.elem[i+1]; } this.usize--; }
獲取順序表長(zhǎng)度
public int size(){ return this.usize; }
清空順序表
順序表長(zhǎng)度直接為0
public void clear(){ this.usize = 0; }
實(shí)現(xiàn)這個(gè)順序表
定義一個(gè)測(cè)試類,測(cè)試這些函數(shù)的輸出
public class TestDemo { public static void main(String[] args) { MyArrayList myArrayList = new MyArrayList(); //給這個(gè)順序表寫入1,2,3,4,5 myArrayList.add(0,1); myArrayList.add(1,2); myArrayList.add(2,3); myArrayList.add(3,4); myArrayList.add(4,5); //打印這個(gè)順序表 myArrayList.display(); //判定5這個(gè)元素是否在該順序表中 System.out.println(myArrayList.contains(5)); //查找5這個(gè)元素 返回它的位置 System.out.println(myArrayList.search(5)); //獲取3位置的元素 System.out.println(myArrayList.getPos(3)); //將4位置的元素重新賦值為9 myArrayList.setPos(4,9); //打印新的順序表 myArrayList.display(); //刪除第一次出現(xiàn)的元素4 myArrayList.remove(4); //打印新的順序表 myArrayList.display(); //獲取順序表的長(zhǎng)度 System.out.println(myArrayList.size()); System.out.println("清空"); //清空順序表 myArrayList.clear(); //打印新的順序表 myArrayList.display(); } }
得到結(jié)果:
順序表的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):順序表查找方便,知道這個(gè)元素的位置就可以直接找到這個(gè)元素。
缺點(diǎn):擴(kuò)容一般成2倍增長(zhǎng),會(huì)有一定的空間浪費(fèi)。
相關(guān)文章
Spring Boot整合JWT的實(shí)現(xiàn)步驟
本文主要介紹了Spring Boot整合JWT,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08JAVA實(shí)現(xiàn)Date日期加一天具體方法
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)Date日期加一天的相關(guān)資料,因?yàn)樵陧?xiàng)目中遇到了需要將日期進(jìn)行加減一些天數(shù)的操作,文中給出了簡(jiǎn)單的代碼示例,需要的朋友可以參考下2023-07-07java代理模式(靜態(tài)代理、動(dòng)態(tài)代理、cglib代理)
代理(Proxy)是一種設(shè)計(jì)模式,提供了對(duì)目標(biāo)對(duì)象另外的訪問方式;這篇文章主要介紹了Java 中的三種代理模式,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解
這篇文章主要介紹了Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解,CountDownLatch是一個(gè)同步工具類,它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下2023-12-12Java使用枚舉實(shí)現(xiàn)狀態(tài)機(jī)的方法詳解
這篇文章主要介紹了Java使用枚舉實(shí)現(xiàn)狀態(tài)機(jī)的方法詳解,枚舉類型很適合用來實(shí)現(xiàn)狀態(tài)機(jī),狀態(tài)機(jī)可以處于有限數(shù)量的特定狀態(tài),它們通常根據(jù)輸入,從一個(gè)狀態(tài)移動(dòng)到下一個(gè)狀態(tài),但同時(shí)也會(huì)存在瞬態(tài),需要的朋友可以參考下2023-11-11