为了账号安全,请及时绑定邮箱和手机立即绑定

线性表报错

线性表报错

慕先生4463397 2017-12-28 22:54:08
package com.线性表.顺序表;/**  * 顺序表ArrayList,用数组表示。一组连续的地址空间8  * @author LH-PC9  * @param <E>10  */ public class ArrayLists {     private Object[] data = null; //data 用来保存此线性表的数据域     private int length; //线性表的容量     private int current;//实际表长          //设置长度 public ArrayLists(int a){    if(a>=0){        length=a;        this.data=new Object[a];        current=a;    }else{        throw new IndexOutOfBoundsException("初始长度不能小于0");    } }public ArrayLists(){     this(10);}//判断表容量是否超出预定大小,如果超出将自动扩充容量public void expansion(){    if(current>=length){        length=current*2;}}//添加元素public void add(Object a){    expansion();    this.data[++current]=a;}//删除元素public void remove(int index){    if(index>=current){        throw new IndexOutOfBoundsException("下标超出表长");    }    for(int i=0;i<current-1;i++){        data[i]=data[i+1];    }    data[current-1]=null;    --current;}//按下标返回元素值public boolean get(int i){    if(i>=current&&i<0){        throw new IndexOutOfBoundsException("输入的下标不存在");    }else{        System.out.println(data[i]);        return true;    }    }//返回实际表长public int size(){    return this.current;}//返回容量public void length(){    System.out.println(this.length);}public String isEmpty(){    if(this.current==0){        return "空";    }else{    return "非空";        }    }public static void main(String[] args) {    ArrayLists  arr1 =new ArrayLists(122);    arr1.add("java");    arr1.add("php");    arr1.add("c");    arr1.add("c++");    for(int i=0;i<arr1.size();i++){        System.out.print(arr1.get(i));    }} }代码35行报错不知道为什么
查看完整描述

2 回答

已采纳
?
慕粉3884565

TA贡献17条经验 获得超2个赞

你这个设置了length,他就是数组长度?
查看完整回答
反对 回复 2017-12-29
?
铭毅

TA贡献4条经验 获得超0个赞

上正确格式的代码?


查看完整回答
反对 回复 2017-12-29
  • 2 回答
  • 0 关注
  • 1393 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信