Java学习day12

1.实现对顺序表的查找插入删除越界判断的实现
1.1 查找某个值在顺序表中的位置,查找成功则返回位置,如果失败,则返回-1

1.2判断线性表是否已满, 检查插入的位置是否合法,如果不合法,则返回失败。

1.3 删除某个位置的元素。判断删除的位置是否合法,不合法,则返回失败。


2.代码
 

package demo1;

public class SequentialList {
	public static final int MAX_LENGTH = 10;
	 
	int length;
	int[] data;
 
	public SequentialList(int[] paraArray) {// 有参数的构造方法,将paraArray赋值给data
		data = new int[MAX_LENGTH];
		length = paraArray.length;
 
		// Cope data.
		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		} // Of for i
	}// Of the second constructor
 
	public String toString() {// 重写toString方法
		String resultString = "";
		if (length == 0) {
			return "empty";
		} // Of if
		for (int i = 0; i < length - 1; i++) {
			resultString += data[i] + ",";
		} // Of for i
		resultString += data[length - 1];
		return resultString;
	}// Of toString
 
	public void reset() {
		length = 0;
	}// Of reset
 
	public int indexOf(int paraValue) {
		int tempPosition = -1;
		for (int i = 0; i < length; i++) {
			if (data[i] == paraValue) {
				tempPosition = i;
				break;
			} // Of if
		} // Of for i
 
		return tempPosition;
	}// Of indexOf
 
	// 插入/
	public boolean insert(int paraPosition, int paraValue) {
		if (length == MAX_LENGTH) {
			System.out.println("List full.");
			return false;
		} // Of if
 
		// 越界检查
		if ((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position " + paraPosition + " is out of bounds.");
			return false;
		} // Of if
 
		for (int i = length; i > paraPosition; i--) {
			data[i] = data[i - 1];
		} // Of for i
 
		data[paraPosition] = paraValue;
		length++;
 
		return true;
	}// Of insert
 
	// 删除///
	public boolean delete(int paraPosition) {
		// 越界检查
		if ((paraPosition < 0) || (paraPosition >= length)) {
			System.out.println("The position " + paraPosition + " is out of bounds.");
			return false;
		} // Of if
 
		// From head to tail.
		for (int i = paraPosition; i < length - 1; i++) {
			data[i] = data[i + 1];
		} // Of for i
 
		length--;
 
		return true;
	}// Of delete
 
	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);
		System.out.println("After initialization, the list is: " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);
 
		int tempValue = 4;
		int tempPosition = tempFirstList.indexOf(tempValue);
		System.out.println("The position of " + tempValue + " is " + tempPosition);
 
		tempValue = 5;
		tempPosition = tempFirstList.indexOf(tempValue);
		System.out.println("The position of " + tempValue + " is " + tempPosition);
 
		tempPosition = 2;
		tempValue = 5;
		tempFirstList.insert(tempPosition, tempValue);
		System.out.println(
				"After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);
 
		tempPosition = 8;
		tempValue = 10;
		tempFirstList.insert(tempPosition, tempValue);
		System.out.println(
				"After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);
 
		tempPosition = 3;
		tempFirstList.delete(tempPosition);
		System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);
 
		for (int i = 0; i < 8; i++) {
			tempFirstList.insert(i, i);
			System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);
		} // Of for i
 
		tempFirstList.reset();
		System.out.println("After reset, the list is: " + tempFirstList);
	}// of main

运行结果如下

 总结

此次是对上次作业的增删改查,对代码的改变类似于数据结构学的增删改查

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值