日撸代码300行学习笔记 Day 12

1.实现对顺序表的查找插入删除越界判断的实现

1.1 查找某个值在顺序表中的位置,查找成功则返回位置,如果失败,则返回-1

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

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


2.代码

package demo;

public class SequentialList_Day2 {

	public static final int MAX_LENGTH = 10;

	int length;
	int[] data;

	public SequentialList_Day2(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_Day2 tempFirstList = new SequentialList_Day2(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

}// of class SequentialList_Day2

运行结果:

3.总结

在下面这一段话中,自身理解还不怎么足,不是很懂其中的含义。

函数 要求同样的输入参数获得同样的输出结果, 但 方法 所依赖的数据既包括参数列表中给出的,也依赖于对象的成员变量. 因此, 面向对象所涉及的参数列表要短些. 例如, locate 方法就有效利用了 length 和 data 这两个成员变量.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值