java数据结构 顺序表

不多说上代码 有注释

Java中顺序表的实现

import java.util.Arrays;

public class SeqList {
        public int[] elem;//null
        public int usedSize;//0
        //设置默认容量
        public static final int DEFAULT_SIZE = 3;
        //构造方法
	public SeqList() {
	    this.elem = new int[DEFAULT_SIZE];
	    this.usedSize = 0;
	}
	//顺序表扩容
	//当顺序表满了的时候就进行扩容
	private void grow() {
	    //数组的拷贝,将长度增加了一倍
	    this.elem = Arrays.copyOf(this.elem,this.elem.length + 1);
	}

	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(isFull()) {
	        //数组扩容
	        grow();
	        /*System.out.println("顺序表满了");
	        return;*/
	    }
	    if(pos < 0 || pos > this.usedSize) {
	        /*System.out.println("pos位置不合法");
	        return;*/
	        throw new ArrayIndexOutOfBoundsException("pos位置不合法");
	    }
	    for (int i = this.usedSize - 1; i >= pos ; i--) {
	        this.elem[i+1] = this.elem[i];
	    }
	    this.elem[pos] = data;
	    this.usedSize ++;
	}
	//判定是否包含这个元素
	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 -1;*/
	        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()) {
	         /*System.out.println("顺序表为空");
	         return -1;*/
	         throw new RuntimeException("顺序表为空");
	     }
	     checkPos(pos);
	     return this.elem[pos];
	 }
	//给pos位置的元素设为value
	public void setPos(int pos,int value) {
	    if(isEmpty()) {
	        /*System.out.println("顺序表为空");
	        return;*/
	        throw new RuntimeException("顺序表为空");
	    }
	    checkPos(pos);
	    this.elem[pos] = value;
	}
	//删除第一次出现的关键字key
	public void remove(int key) {
	   //1,调用search看返回值 -1没有key 那木就删除不要了
	    //2, 调换覆盖key关键字
	    //3,size = size - 1
	    if(isEmpty()) {
	         /*System.out.println("顺序表为空");
	        return;*/
	        throw new RuntimeException("顺序表为空");
	    }
	    if(search(key) == -1) {
	        System.out.println("没有关键字");
	        return;
	    }
	    for (int i = search(key); i < this.usedSize - 1 ; i++) {
	        this.elem[i] = this.elem[i+1];
	    }
	    this.usedSize = this.usedSize-1;
	}
	//获取顺序表长度
	public int size() {
	   return this.usedSize;
	}
	//清空顺序表
	public void clear() {
	   this.usedSize = 0;
	}
      //这几种方法只在类中使用 所以用private封装
        //顺序表是否为满
        private boolean isFull() {
            if(this.usedSize == this.elem.length) {
                return true;
            }
            return false;
        }
        //顺序表是否为空
        private boolean isEmpty() {
            if(this.usedSize == 0) {
                return true;
            }
            return false;
        }
        //检查pos的位置是否合法
        private void checkPos(int pos) {
            if(pos < 0 || pos > this.usedSize) {
                /*System.out.println("pos位置不合法");
                return;*/
                throw new ArrayIndexOutOfBoundsException("pos位置不合法");
            }
        }
}
								
		```
			
### Java顺序表数据结构 #### 顺序表的概念 在Java中,顺序表是一种线性数据结构,它通过数组来存储元素。由于数组具有连续内存的特点,因此可以快速访问任意位置的元素。然而,插入和删除操作可能较为耗时,因为这些操作通常需要移动大量元素以保持顺序[^2]。 #### 创建顺序表类 为了实现一个简单的顺序表,定义`MyArrayList`类作为容器: ```java public class MyArrayList { private int[] array; private int usedsize; public MyArrayList() { this.array = new int[10]; this.usedsize = 0; } } ``` 这段代码初始化了一个固定大小为10的整型数组,并设置了当前使用的长度为零。 #### 添加元素到顺序表顺序表添加新项的方法如下所示: ```java public void add(int value){ if (usedsize >= array.length) { resize(); } array[usedsize++] = value; } private void resize(){ int newSize = array.length * 2; int[] newArray = new int[newSize]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } ``` 这里实现了动态扩容机制,当列表已满时会自动增加容量一倍。 #### 查找特定值的存在情况 要检查某个给定数值是否存在於顺序表内,则需遍历整个序列直到找到匹配项为止: ```java public boolean contains(int toFind){ for (int i = 0; i < usedsize; i++){ if (array[i] == toFind){ return true ; } } return false; } ``` 此方法返回布尔类型的真或假表示目标数是否存在于集合之中[^1]。 #### 显示所有元素 最后提供一种方式打印出所有的成员变量: ```java public void display(){ for (int i = 0; i < this.usedsize; i++){ System.out.print(this.array[i]+" "); } System.out.println(); } ``` 上述函数利用循环迭代器逐一遍历并输出每一个有效位上的内容[^3]。 #### 测试程序实例 下面给出完整的测试案例用于验证以上功能模块的工作状态: ```java public class Test { public static void main(String[] args) { MyArrayList arrayList = new MyArrayList(); arrayList.add(5); arrayList.add(6); arrayList.add(10); arrayList.add(9); System.out.println("
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值