函数
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)所用方法不同却不能改不同名字?