Java顺序表

1.java顺表本质可以粗略的同数组结合

2.不同的是多了length这些属性,其对象数据类型保持一致,储存在数组中。

这是代码示例理解

3.属性:数组一个,长度计量

/**
 * 
 */
package datastructure.list;

/**
 ***************************
 * TODO Sequential list
 * 
 * @author Chen Fan
 * @version 1.0 time 2021年12月19日
 ****************************
 */
public class SequentialList {
	/**
	 * The maximal of the list. It is a constant.
	 */
	public static final int MAX_LENGTH = 10;
	/**
	 * The actual Length not exceeding MAX_LENGTH.Attention: Length is not only the
	 * member variable of Sequential list, but also the member variable of Array. In
	 * fact, a name can be the member of different class.
	 */
	int length;
	/**
	 * The data stored in an array.
	 */
	int[] data;

	/**
	 * Construct an empty sequential list.
	 */
	public SequentialList() {
		length = 0;
		data = new int[MAX_LENGTH];
	}// Of the fist constructor.

	/**
	 *********************
	 * Construct a sequential list using an array.
	 * 
	 * @param paraArray The given array. Its length should not exceed MAX_LENGTH.
	 *                  For simplicity now we do not check it.
	 *********************
	 */
	public SequentialList(int[] paraArray) {
		data = new int[MAX_LENGTH];
		length = paraArray.length;

		// Copy data.
		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		} // Of for i
	}// Of the second constructor

	/**
	 * Overrides the method claimed in Object, the superclass of any class
	 */
	public String toString() {
		String resultString = "";

		if (length == 0) {
			return "empty";
		}
		for (int i = 0; i < length - 1; i++) {
			resultString += data[i] + ",";
		} // Of for i

		resultString += data[length - 1];

		return resultString;
	}// Of toString

	/**
	 * 
	 *********************
	 * TODO Reset to empty.
	 * 
	 *********************
	 */
	public void reset() {
		length = 0;
	}// Of reset

	/**
	 * 
	 *********************
	 * TODO The entrance of the program
	 * 
	 * @param args
	 *********************
	 */
	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);
		System.out.println("Intitialized, the list is : " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);

		tempFirstList.reset();
		System.out.println("After reset, the list is: " + tempFirstList);
	}// Of main
}// Of class SequentialList.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值