顺序表的功能的代码实现

it2023-07-19  76

顺序表的功能实现代码总结

import java.util.Arrays; public class ArrayList { public int[] array; public int usedSize; //顺序表有效元素个数 //底层由数组实现 public ArrayList() { System.out.println("hahaha"); } public ArrayList(int[] array) { this.array = array; } public ArrayList(int[] array, int usedSize) { this.array = array; this.usedSize = usedSize; } //判断链表是否满 public boolean isFull(){ if(this.usedSize == this.array.length){ // System.out.println("链表已满"); return true; } return false; } //扩容链表 public void resize(){ if(isFull()){ this.array = Arrays.copyOf(this.array,this.array.length * 2); } } // 在 pos 位置新增元素 public void add(int pos, int newNumber){ if(this.array == null || this.array.length == 0){ System.out.println("链表为空 增加失败"); return; } if(pos < 0 || pos > this.usedSize){ System.out.println("位置错误 增加失败"); return; } if(isFull()){ // System.o // ut.println("链表已满 增加失败"); resize(); } for(int i = this.usedSize - 1; i >= pos; i--){ this.array[i+1] = this.array[i]; } this.array[pos] = newNumber; this.usedSize++; // System.out.println(Arrays.toString(this.array)); systemArrayList(); } // 打印顺序表 public void systemArrayList(){ if(this.usedSize >= 0) { for (int i = 0; i < this.usedSize; i++) { System.out.print(this.array[i] + " "); } System.out.println(); } else { System.out.println("该链表为空"); return; } } // 判定是否包含某个元素 public boolean contains(int number){ for(int i = 0; i < this.usedSize; i++){ if(this.array[i] == number){ return true; } } return false; } // 查找某个元素对应的位置 public int search(int number){ for(int i = 0; i < this.usedSize; i++){ if(this.array[i] == number){ return i; } } return -1; } // 获取 pos 位置的元素 public int find(int pos){ if(pos < 0 || pos > this.usedSize){ System.out.println("位置错误 无法寻找"); System.out.println(); return -1; } return this.array[pos]; } // 给 pos 位置的元素设为 value public void set(int pos, int value){ if(pos < 0 || pos > this.usedSize){ System.out.println("位置错误 无法寻找"); System.out.println(); return; } this.array[pos] = value; // System.out.println(Arrays.toString(this.array)); systemArrayList(); } //删除第一次出现的关键字key public void del(int key){ if(contains(key)){ for(int i = search(key); i < this.usedSize - 1; i++){ this.array[i] = this.array[i + 1]; } this.usedSize--; if(this.usedSize == 0){ System.out.println("null"); } systemArrayList(); } else { System.out.println("没有找到该值"); return; } } // 获取顺序表长度 public int getUsedSize(){ if(this.array == null || this.array.length == 0){ System.out.println("链表为空 增加失败"); return -1; } return this.usedSize; } // 清空顺序表 public void clean(){ this.usedSize = 0; systemArrayList(); } public static void main(String[] args){ int[] array = {5,5,3,4,5,6}; ArrayList arrayList = new ArrayList(array,6); arrayList.add(5,10); arrayList.systemArrayList(); System.out.println(arrayList.contains(5)); System.out.println(arrayList.search(5)); System.out.println(arrayList.find(3)); arrayList.set(3,5); arrayList.del(4); System.out.println(arrayList.getUsedSize()); // arrayList.clean(); //删除所有key值 for(int i = arrayList.usedSize - 1; i >= 0; i--){ // arrayList.systemArrayList(); arrayList.del(5); if(arrayList.contains(5) == false){ System.out.println("关键字为5的数值都被删除完了"); break; } } } }
最新回复(0)