集合框架中用ArrayList和Vector来实现顺序表这个数据结构,他们的底层都是基于数组来实现的线性表,这两顺序表的区别在于:
- Vector是一个老版本的顺序表 ArrayList是新版本的顺序表
- Vector是线程安全的(效率比线程不安全版本低).ArrayList是线程安全的,不加锁的
- 现在不太推荐使用Vector.更建议使用JUC中的组件,优化的更好,性能更高
顺序表的特性就是知道我放了几个元素 并且不能跳着放
接下来我们就借助数组来自己实现一个顺序表:
public class MyArrayList {
private int[] elem;//null
private int usedSize;//0
//设置默认容量
private static final int DEFAULT_SIZE=4;
public MyArrayList(){
this.elem=new int[DEFAULT_SIZE];
this.usedSize=0;
}
// 打印顺序表
public void display() {
for(int i=0;i<this.usedSize;i++){
System.out.print(this.elem[i]+" ");
}
}
// 在 pos 位置新增元素
//1.判断当前顺序表是不是满的
//2.挪数据从顺序表的最后一个元素开始往后挪.一直挪到POS位置
//在usedSize-1的位置开始挪位置,一直到POS位置
public void add(int pos, int data) {
if(isFull()){
//System.out.println("顺序表是满的!");
//return;
grow();
}
if(pos<0||pos>this.usedSize){
//System.out.println("pos位置不合法");
//return;
//抛出异常也可以
throw new ArrayIndexOutOfBoundsException("pos位置不合法");
}
int i=this.usedSize-1;
while(i>=pos){
this.elem[i+1]=this.elem[i];
i--;
}
this.elem[pos]=data;
this.usedSize+=1;
}
//判断是不是满的
public boolean isFull(){
if(this.usedSize==this.elem.length){
return true;
}
return false;
}
//判断顺序表是否为空
public boolean isEmpty(){
if(this.usedSize==0){
return true;
}
return false;
}
// 判定是否包含某个元素
public boolean contains(int toFind) {
if (isEmpty()) {
//System.out.println("顺序表为空!");
//return false;
throw new RuntimeException("顺序表为空!");
}
for (int i = 0; i < this.usedSize; i++) {
if (toFind == this.elem[i]) {
return true;
}
}
return false;
}
// 查找某个元素对应的位置
public int search(int toFind) {
if (isEmpty()) {
//System.out.println("顺序表为空!");
//return false;
throw new RuntimeException("顺序表为空!");
}
for (int i = 0; i < this.usedSize; i++) {
if (toFind == this.elem[i]) {
return i;
}
}
return -1;
}
// 获取 pos 位置的元素
public int getPos(int pos) {
if(isEmpty()){
throw new RuntimeException("顺序表为空!");
}
if(pos<0||pos>this.usedSize){
throw new ArrayIndexOutOfBoundsException("pos位置不合法");
}
return this.elem[pos];
}
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int key) {
if(key<0||key>=this.usedSize){
throw new ArrayIndexOutOfBoundsException();
}
if(search(key)==-1){
return;
}
int i=search(key);
while(i<this.usedSize-1){
this.elem[i]=this.elem[i+1];
i++;
}
this.usedSize-=1;
}
// 获取顺序表长度
public int size() {
return this.usedSize; }
// 清空顺序表
//置为0
public void clear() {
this.usedSize=0;
}
//扩容 不可能在原来基础上扩容
private void grow(){
this.elem=Arrays.copyOf(this.elem,this.elem.length*2);
}
}
以上就是关于自己实现的顺序表的增删改查基础操作,在集合框架中的线性表ArrayList,Vector,LinkedList都是实现了List这个接口,我们在下一篇博客中将会介绍一下java.util包下List接口的基本操作~