快捷导航

用java实现顺序表报异常

package 顺序表;
public class SqList
{
    public Object[] listElem;//线性表存储空间
    private int lengths;//线性表的长度

    //顺序表构造函数,构造有个长度为maxSize的线性表
    public SqList(int maxSize){
        lengths=0;
        listElem = new Object[maxSize];
    }
    //置空操作
    public void clear(){
        lengths=0;
    }
    //判断长度是否为0,0即是空表
    public boolean isEmpty(){
            if(lengths>0){
                System.out.println("非空");
            }
            return lengths==0;
    }
    //取表长度,返回lengths的长度
    public int length(){
        return lengths;
    }
    //取表元素
    public Object get(int i)throws Exception{
        //如果不合法报异常
        if(i>0 || i>lengths-1){
            throw new Exception("第"+i+"个元素不存在");
        }
        return listElem;
    }
    //插入操作
    public void insert(int i,Object x) throws Exception{
        if(lengths==listElem.length){
            throw new Exception("顺序表已满");
        }
        if(ilengths){
            throw new Exception("插入位置不合法");
        }
        //从尾部往前扫
        for(int j=lengths;j>i;j--){
            listElem[j]=listElem[j-1];
            listElem=x;
            lengths++;
        }
    }
        //删除操作
        public void remove(int i)throws Exception{
            if(ilengths-1){
                throw new Exception("删除位置不合法");
            }
               
            //下标移动要出删除的i处
                for(int j=i;j

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP