Java----动态顺序表的接口实现

MyArrayList.java

public class MyArrayList {
	/**
	 * 顺序表
	 * 顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储
	 * 顺序表底层是一个数组,但顺序表本身是一种数据结构,新的类型
	 *      能知道放了多少元素
	 *      不能跳着放元素
	 */
	public int[] elem ;
	public int usedSize;
	/**
	 * 设置默认容量
	 */
	public  static final int DEFAULT_SIZE = 10;
	public MyArrayList() {
		this.elem = new int[DEFAULT_SIZE];
		this.usedSize = 0;
	}
	/**
	 * 要点
	 * 1.判断当前顺序表是不是满的
	 * 2.挪数据:从顺序表最后一个元素开始往后挪
	 *   usedSize-1 开始挪数据,一直挪到pos位置
	 */
	/**
	 * 判断当前顺序表是不是满的
	 */
	public  boolean isFull() {
		if(usedSize == this.elem.length) {
			return true;
		}
		return false;
	}
	/**
	 * 判断当前顺序表是不是空的
	 */
	public boolean isEmpty() {
		if(this.usedSize == 0) {
			return true;
		}
		return false;
	}
	/**
	 * 打印顺序表
	 */
	public void display() {
		for (int i = 0; i < usedSize ; i++ ) {
			System.out.print(elem[i]+" ");
		}
		System.out.println();

	}
	/**
	 *在 pos 处增加新元素
	 */
	public void add(int pos,int data) {
		if(isFull()) {
			throw new RuntimeException("顺序表是满的!");
		}
		if (pos<0 || pos > this.usedSize) {
			throw new ArrayIndexOutOfBoundsException("pos位置不合法!");
		}
		for (int i = 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()) {
			throw new RuntimeException("顺序表为空!");
		}
		for (int i = 0; i < this.usedSize ; i++ ) {
			if(this.elem[ i ] == toFind)  {
				return true;
			}
		}
		return false;
	}
	/**
	 *查找某个元素对应的位置
	 */
	public int search(int toFind) {
		if(isEmpty()) {
			throw new RuntimeException("顺序表为空!");
		}
		for (int i = 0; i < this.usedSize ; i++ ) {
			if(this.elem[ i ] == toFind)  {
				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) {
		if (pos<0 || pos > this.usedSize) {
			throw new ArrayIndexOutOfBoundsException("pos位置不合法!");
		}
		this.elem[pos] = value;
	}
	/**
	 * 	获取顺序表长度
	 */
	public int size() {
		int l = elem.length;
		return l;
	}
	/**
	 * 	清空顺序表
	 */
	public void clear() {
		this.usedSize=0;
	}
	/**
	 * 	删除第一次出现的关键字key
	 */
	public void remove(int key) {
		if( search(key) != -1) {
			for( int i=search(key); i < this.usedSize-1;i++ ) {
				this.elem[i]= this.elem[i+1];
			}
			this.usedSize--;
		}
	}
	/**
	 * 扩容
	 */
	public void grow() {
		this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
	}
}

TestDemo.java

public class TestDemo {
	public static void main(String[] args) {
		MyArrayList myArrayList =  new MyArrayList();
		{myArrayList.add(0,10);
		myArrayList.add(1,20);
		myArrayList.add(2,30);
		myArrayList.add(3,40);
		myArrayList.add(4,50);
		myArrayList.add(5,60);
		myArrayList.add(6,70);
		myArrayList.add(7,80);
		myArrayList.add(8,90);
		myArrayList.add(9,100);}
		System.out.println("==================================");
		System.out.println("原始顺序表:");
		myArrayList.display();
		System.out.println("==================================");
		System.out.println("扩容");
		myArrayList.grow();
		myArrayList.add(10,101);
		myArrayList.display();
		System.out.println("==================================");
		System.out.println("是否包含 3  :"+myArrayList.contains(3));
		System.out.println("==================================");
		System.out.println("在表里寻找 3  :"+myArrayList.search(3));
		System.out.println("==================================");
		System.out.println("找到 表中 2 位置的数据:"+myArrayList.getPos(2));
		System.out.println("==================================");
		System.out.println("顺序表长度:"+myArrayList.size());
		System.out.println("==================================");
		System.out.println("更改 3 处的元素为 100");
		myArrayList.setPos(3,100);
		myArrayList.display();
		System.out.println("==================================");
		System.out.println("删除");
		myArrayList.remove(100);
		myArrayList.display();
		System.out.println("==================================");
		System.out.println("清除");
		myArrayList.clear();
		myArrayList.display();
		System.out.println("==================================");
	}
}

运行结果

=================================
原始顺序表:
10 20 30 40 50 60 70 80 90 100 
==================================
扩容
10 20 30 40 50 60 70 80 90 100 101 
==================================
是否包含 3  :false
==================================
在表里寻找 3  :-1
==================================
找到 表中 2 位置的数据:30
==================================
顺序表长度:20
==================================
更改 3 处的元素为 100
10 20 30 100 50 60 70 80 90 100 101 
==================================
删除
10 20 30 50 60 70 80 90 100 101 
==================================
清除

==================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

twelve1959

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值