package org.xzr.chapter2.book;
public class SeqList implements List {
final int defaultSize=10;
int maxSize;
int size;
Object[] listArray;
public SeqList(){
initiate(defaultSize);
}
public SeqList(int size){
initiate(size);
}
public void initiate(int sz){
maxSize=sz;
size=0;
listArray=new Object[sz];
}
public Object delete(int i) throws Exception {
// TODO 自动生成方法存根
if(size==0){
throw new Exception("顺序表已空无法删除1");
}
if(i<0||i>size-1){
throw new Exception("参数错误!");
}
Object it=listArray[i];
for(int j=i;j<size-1;j++){
listArray[j]=listArray[j+1];
}
size--;
return it;
}
public Object getData(int i) throws Exception {
// TODO 自动生成方法存根
if(i<0||i>=size){
throw new Exception("参数错误");
}
return listArray[i];
}
public void insert(int i, Object obj) throws Exception {
// TODO 自动生成方法存根
if(size==maxSize){
throw new Exception("顺序表已满无法插入!");
}
if(i<0||i>size){
throw new Exception("参数错误!");
}
for(int j=size;j>i;j--){
listArray[j]=listArray[j-1];
}
listArray[i]=obj;
size++;
}
public boolean isEmpty() {
// TODO 自动生成方法存根
return size==0;
}
public int size() {
// TODO 自动生成方法存根
return size;
}
public int moreDataDelete(SeqList L,Object x)throws Exception{
int i;
int tag=0;
for(i=0;i<L.size;i++){
L.delete(i);
i--;
tag=1;
}
return tag;
}
}
评论5
最新资源