62,628
社区成员
发帖
与我相关
我的任务
分享public class SQList implements IList{
private Object []listElem;
private int curLen;
private int maxSize;
public SQList(int size) { // 构造函数,初始化一个空顺序表
maxSize = size;
curLen = 0;
listElem = new Object[maxSize];
}
public void insert(int i, Object x)throws Exception
{ // 在第i个元素前插入x,1 <= i <= n+1(n为当前元素个数,即curLen)
if(curLen == maxSize) {
throw new Exception("顺序表已满!!!");
}
if(i < 0 || i > curLen) {
throw new Exception("插入位置不合法!!!");
}
for(int j = curLen; j >i ; j--) { // 从后向前后移一个位置
listElem[j] = listElem[j-1];
}
listElem[i] = x;
++curLen;
}
}