public String toString() {
if (size == 0) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i].toString() + ",");
}
int len = sb.length();
return sb.delete(len - 2, len).append("]").toString();
}
}
}
测试模拟线性表的基本操作:
package com.ietree.basic.datastructure.Sequence;
/**
* 测试类
*
* @author Dylan
*/
public class SequenceListTest {
public static void main(String[] args) {
SequenceList<String> list = new SequenceList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.insert("eee", 1);
System.out.println(list);
list.delete(2);
System.out.println(list);
System.out.println("ccc在顺序线性表中的位置:" + list.locate("ccc"));
}
}
程序输出:
[aaa,eee,bbb,ccc,dd]
[aaa,eee,ccc,dd]
ccc在顺序线性表中的位置:2