day12;
顺序表(二)
顺序表的查找、插入、删除
总结:
1、在插入的时候,需要进行对表满值判断,若已满或者位置越界,就返回false。
2、在删除的时候需要判断位置是否合法。
代码:
package day12;
public class SequentialList {
public static final int MAX_LENGTH =10;
int length;
int []data;
//无参方法需要保持方法名和类名的一致性;
public SequentialList()
{
length =0;
data= new int [MAX_LENGTH];
}
//方法重载,方法名称与类名称保持一致;
public SequentialList (int [] paraArray)
{
data = new int [MAX_LENGTH];
length =paraArray.length;
for (int i=0;i<paraArray.length;i++)
{
data[i]=paraArray[i];
}
}
//由data[]的数字数组变成 resultString[]的字符串;
public String toString()
{
String resultString ="";
if(length == 0 )
{
return "empty";
}
for (int i=0;i<length - 1;i++)
{
resultString += data[i]+",";
}
resultString += data [length - 1];
return resultString;
}
//重置为空
public void reset() {
length = 0;
}
//查找
public int indexOf(int paraValue)
{
int tempPosition = -1;
for (int i=0;i<length;i++)
{
if(data[i] == paraValue)
{
tempPosition = i;
break;
}
}
return tempPosition;
}
//插入
public boolean insert(int paraPosition, int paraValue)
{
if(length == MAX_LENGTH)
{
System.out.println("List full.");
return false;
}
if((paraPosition < 0)||(paraPosition > length))
{
System.out.println("The position "+paraPosition +"is out of bounds.");
return false;
}
for (int i =length;i>paraPosition;i--)
{
data[i] =data[i -1];
}
data[paraPosition] = paraValue;
length++;
return true;
}
//删除
public boolean delete (int paraPosition)
{
if((paraPosition <0 )||(paraPosition >= length))
{
System.out.println("The position" + paraPosition + "is out of bounds.");
return false;
}
for (int i=paraPosition;i<length-1;i++)
{
data[i]=data[i+1];
}
length--;
return true;
}
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.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);
}
}
结果: