Java代码实现:数组顺序表 增加、删除、修改、查询、插入

就插入方法烧了一点时间,有兴趣可以研究研究。

//接口里面还可自添 排序等方法
interface IList {
	void Print_List();// 打印数组

	void Length_List();// 有效数据长度

	void Add_List(int data);// 向数组添加数据

	int Locate_List(int data);// 按值查询

	int Delete_List(int data);// 删除数据

	int Update_List(int Olddata, int Newdata);// 修改数据

	int preInsert_List(int data, int location);// 按下标插入

}

public class SetList implements IList {

	private static final int MAXSIZE = 100;
	private static int[] intarray;
	private int effectiveData = 0;// 输入有效数据总数

	public SetList() {
		intarray = new int[MAXSIZE];
	}

	public static void main(String[] args) {

		SetList user = new SetList();
		user.Add_List(10);
		user.Add_List(15);
		user.Add_List(20);
		user.Add_List(28);
		user.Add_List(30);
		user.Add_List(34);
		user.Print_List();
		// 添加
		user.Add_List(40);
		user.Print_List();
		// 查找
		user.Locate_List(30);
		// 删除
		user.Delete_List(10);
		user.Print_List();
		// 修改
		user.Update_List(34, 38);
		user.Print_List();
		// 插入
		user.preInsert_List(45, 7);
		user.Print_List();
		//

	}

	public void Print_List() {
		// 输出数组
		for (int i = 0; i < effectiveData; i++) {
			System.out.print(intarray[i] + " ");
		}
		System.out.println();
	}

	public void Length_List() {
		System.out.println("共有效数据:" + effectiveData);
	}

	public void Add_List(int data) {// 添加数据
		if (effectiveData == MAXSIZE - 1)
			System.out.println("List Full");
		intarray[effectiveData] = data;
		effectiveData++;
	}

	public int Locate_List(int data) {// 查找,返回下标
		int i;
		for (i = 0; i < effectiveData; i++) {
			if (intarray[i] == data) {
				break;
			}
		}

		if (i == effectiveData) {
			System.out.println("the search failed");
		} else
			System.out.println("下标为:" + i);
		return 0;
	}

	public int Delete_List(int data) {// 删除
		int i;
		for (i = 0; i < effectiveData; i++) {
			if (intarray[i] == data) {
				break;
			}
		}

		if (i == effectiveData) {
			System.out.println("the search failed");
		} else
			for (int j = i; j < effectiveData; j++) {

				intarray[j] = intarray[j + 1];// 后面数据往前面移动
			}
		effectiveData--;// 有效数据减一
		return 0;
	}

	public int Update_List(int Olddata, int Newdata) {// 输入原值和修改值
		int i;
		for (i = 0; i < effectiveData; i++) {
			if (intarray[i] == Olddata) {
				break;
			}
		}

		if (i == effectiveData) {
			System.out.println("the search failed");
		} else
			intarray[i] = Newdata;
		return 0;
	}

	public int preInsert_List(int data, int location) {
		if (effectiveData == MAXSIZE - 1) {
			System.out.println("List Full");
			return 0;
		}
		if (location < 0 || location >= effectiveData + 1) {
			System.out.println("Fatal Error");
			return 0;
		}
		for (int j = effectiveData; j > location - 1; j--) {
			intarray[j + 1] = intarray[j];
		}
		// 赋值到输入下标位置
		intarray[location] = data;
		effectiveData++;// 添加成功自加
		return 0;
	}
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值