顺序表的插值与删除
/**
*
* Locate the given value. If it appears in multiple positions, simply
* return the first one.
*
* @param paraValue
* The given value.
* @return The position. -1 for not found.
*
*/
public int locate(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;
//返回paraValue值所在位置
}// Of locate
/**
*
* Insert a value to a position. If the list is already full, do nothing.
*
* @param paraPosition
* The given value.
* @return Success or not.
*
*/
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
// From tail to head.
for (int i = length; i > paraPosition; i--) {
data[i] = data[i - 1];
//依次往后挪一个位置,下标是由0开始,因此data[i]可储存i+1个数据
}// Of for
data[paraPosition] = paraValue;
length++;
//按理应该在for前先进行length++,才能储存i+1位置的值,为什么这里不报错呢?
return true;
}// Of insert
/**
*
* Delete a value at a position.
*
* @param paraPosition
* The given position.
* @return Success or not.
*
*/
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];
//向左挪一个位置,data[max]点未被重新赋值,因根本没有进入循环
}// Of for
length--;
//length-1时使最右端值索引不到
return true;
}// Of insert
/**
*
* 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);
System.out.println("Initialized, the list is: " + tempFirstList.toString());
System.out.println("Again, the list is: " + tempFirstList);
int tempValue = 4;
int tempPosition = tempFirstList.locate(tempValue);
//由于locate是属于对象的一个方法,需要由"."方式使用
System.out.println("The position of " + tempValue + " is " + tempPosition);
tempValue = 5;
tempPosition = tempFirstList.locate(tempValue);
System.out.println("The position of " + tempValue + " is " + tempPosition);
tempPosition = 2;
tempValue = 5;
tempFirstList.insert(tempPosition, tempValue);
System.out.println("After insering " + 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