一、概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。
在数组上完成数据的增删查改。
顺序表一般可以分为:
静态顺序表:使用定长数组存储。
动态顺序表:使用动态开辟的数组存储。
二、接口实现
我们来实现一个动态顺序表.
import java.util.Arrays; public class MyArrayList { public int[] elem;//null public int usedSize; public MyArrayList() { this.elem = new int[5]; } // 打印顺序表 public void display() { for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i]+" "); } System.out.println(); } // 在 pos 位置新增元素 public void add(int pos, int data) { if(pos < 0 || pos > this.usedSize ) { System.out.println("pos位置不合法!"); return; } //扩容函数 if (isFull()){ System.out.println("扩容"); this.elem = 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(){ if(this.usedSize == this.elem.length){ return true; } return false; } public boolean isEmpty(){ return this.usedSize == 0; } // 判定是否包含某个元素 public boolean contains(int toFind) { if(isEmpty()){ System.out.println("该顺序表为空!"); return false; } for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind) { return true; } } return false; } // 查找某个元素对应的位置 public int search(int toFind) { if(isEmpty()){ System.out.println("该顺序表为空!"); return -1; } int i = 0; for ( 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 >= usedSize) { throw new ArrayIndexOutOfBoundsException("pos位置不合法!"); } return this.elem[pos]; } // 给 pos 位置的元素设为 value //更新 public void setPos(int pos, int value) { if (pos < 0 || pos >= usedSize) { throw new ArrayIndexOutOfBoundsException("pos位置不合法!"); } this.elem[pos] = value; } //删除第一次出现的关键字key public void remove(int toRemove) { int index = search(toRemove); if (index == -1) { System.out.println("你要删的数字:"+toRemove+"没找到"); return; } for (int i = index; i < this.usedSize -1 ; i++) { this.elem[i] = this.elem[i+1]; } usedSize--; System.out.println("删除成功!"); } // 获取顺序表有效长度 public int size(){ return this.usedSize; } // 清空顺序表 public void clear() { for (int i = 0; i < this.usedSize; i++) { this.elem[i] = 0; } this.usedSize = 0; } }
验证结果:
public class Test{ public static void main(String[] args) { MyArrayList2 myArrayList2 = new MyArrayList2();//有了一个顺序表 myArrayList2.add(0,12); myArrayList2.add(1,33); myArrayList2.add(2,88); myArrayList2.display(); System.out.println(myArrayList2.contains(10)); System.out.println(myArrayList2.contains(88)); System.out.println(myArrayList2.search(1)); System.out.println(myArrayList2.search(12)); //System.out.println(myArrayList2.getPos(4)); System.out.println(myArrayList2.getPos(2)); myArrayList2.setPos(1,100); myArrayList2.display(); myArrayList2.remove(13); myArrayList2.display(); myArrayList2.remove(88); myArrayList2.display(); System.out.println(myArrayList2.size()); System.out.println("----------"); myArrayList2.clear(); myArrayList2.display();
}
}
运行结果:
缺点:
1. 顺序表中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。