Java實現(xiàn)一個順序表的完整代碼
實現(xiàn)一個順序表
接口實現(xiàn)
定義一個MyArrayList類,在類中實現(xiàn)以下函數(shù)
public class MyArrayList { }
數(shù)組的定義
public int[] elem;//定義一個整形數(shù)組 public int usize;//usize表示數(shù)組的長度 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位置新增元素
先定義一個isFull函數(shù)判斷順序表是否滿了,滿了返回true,沒滿則返回false
public boolean isFull(){ if (this.usize == this.elem.length){ return true; } return false; }
將pos位置后的元素后移,順序表順序表長度增加一位
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++; }
判定是否包含某個元素
public boolean contains(int key){ for (int i = 0; i < this.usize; i++) { if (this.elem[i] == key){ return true; } } return false; }
查找某個對應(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位置的元素
定義一個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é)束每一項前移,覆蓋掉關(guān)鍵字,長度減少一位
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--; }
獲取順序表長度
public int size(){ return this.usize; }
清空順序表
順序表長度直接為0
public void clear(){ this.usize = 0; }
實現(xiàn)這個順序表
定義一個測試類,測試這些函數(shù)的輸出
public class TestDemo { public static void main(String[] args) { MyArrayList myArrayList = new MyArrayList(); //給這個順序表寫入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); //打印這個順序表 myArrayList.display(); //判定5這個元素是否在該順序表中 System.out.println(myArrayList.contains(5)); //查找5這個元素 返回它的位置 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(); //獲取順序表的長度 System.out.println(myArrayList.size()); System.out.println("清空"); //清空順序表 myArrayList.clear(); //打印新的順序表 myArrayList.display(); } }
得到結(jié)果:
順序表的優(yōu)缺點
優(yōu)點:順序表查找方便,知道這個元素的位置就可以直接找到這個元素。
缺點:擴(kuò)容一般成2倍增長,會有一定的空間浪費。
相關(guān)文章
java代理模式(靜態(tài)代理、動態(tài)代理、cglib代理)
代理(Proxy)是一種設(shè)計模式,提供了對目標(biāo)對象另外的訪問方式;這篇文章主要介紹了Java 中的三種代理模式,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07Java隊列同步器之CountDownLatch實現(xiàn)詳解
這篇文章主要介紹了Java隊列同步器之CountDownLatch實現(xiàn)詳解,CountDownLatch是一個同步工具類,它允許一個或多個線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動框架服務(wù)的線程已經(jīng)啟動所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下2023-12-12Java使用枚舉實現(xiàn)狀態(tài)機(jī)的方法詳解
這篇文章主要介紹了Java使用枚舉實現(xiàn)狀態(tài)機(jī)的方法詳解,枚舉類型很適合用來實現(xiàn)狀態(tài)機(jī),狀態(tài)機(jī)可以處于有限數(shù)量的特定狀態(tài),它們通常根據(jù)輸入,從一個狀態(tài)移動到下一個狀態(tài),但同時也會存在瞬態(tài),需要的朋友可以參考下2023-11-11