day11

函数

package datastructure;

/**
 * Sequential list.
 * 
 * @author Michelle Min mitchellemin@163.com.
 */

public class SequentialList {

	/**
	 * The maximal length of the list. It is a constant.
	 */
	public static final int MAX_LENGTH = 10;
    //这里的final什么意思呢?

	/**
	 * 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 variable of different classes.
	 */
	int length;

	/**
	 * the data stored in an array.
	 */
	int[] data;

	/**
	 * 
	 * Construct an empty sequential list.
	 * @param tempArray 
	 * 
	 */
	public SequentialList() {
		//括号中没有参数的时候,用这个方法
		length = 0;
		data = new int[MAX_LENGTH];
	}// Of the first 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];
			//将数组paraArray的值赋予data[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";
            //这里不需要break么?
		}// Of if

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

		return resultString;
	}// Of toString

	/**
	 * 
	 * Reset to empty
	 * 
	 */
	public void reset() {
		length = 0;
		//由于length = paraArray.length;,为了使length=0成立
		//将paraArray数组内数据销毁,成为野指针,过段时间会被回收
	}// Of reset

	/**
	 * 
	 * The entrance of the program.
	 * 
	 * @param args
	 *            not used now
	 * 
	 */

	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);
		//用这个类声明一个数据类型(与int等同),再进行赋值
		// new 生成新的对象除了八种数据类型其它都需要new
		
		System.out.println("Initialized, 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

 

    问题:

1.构造函数为什么规定SequentialList()和Sequentical(paraArray)所用方法不同却不能改不同名字?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值