请大家看下面的代码,在运行时老是出现
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at OrderArray.insert(OrderedApp.java:43)
at OrderedApp.main(OrderedApp.java:78)
但是我看不出插入模块有什么问题,请大家指点,谢谢!
代码如下:class OrderArray{
private long[] a;
private int nElem;
public OrderArray(int max){
a = new long[max];
nElem = max;
}
public int size(){
return nElem;
}
public int find(long SearchKey){
int LowerBound = 0;
int UpperBound = nElem - 1;
int CurIn;
CurIn = (LowerBound + UpperBound) / 2;
while(true){
if(a[CurIn] == SearchKey){
return CurIn;
}
else if(LowerBound > UpperBound){
return nElem;
}
else{
if(a[CurIn] < SearchKey){
UpperBound = CurIn - 1;
}
else{
LowerBound = CurIn + 1;
}
}
}
}
public void insert(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] > value){
break;
}
}
for(int k = nElem;k > j;k --){
a[k] = a[k - 1];
}
a[j] = value;
nElem ++;
}
public boolean delete(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] == value){
break;
}
}
if(j == nElem){
return false;
}
else{

for(int k = j;k < nElem;k ++){
a[k] = a[k + 1];
}
nElem --;
return true;
}
}
public void display(){
for(int j = 0;j < nElem;j ++){
System.out.print(a[j] + " ");
}
System.out.println("");
}
}
public class OrderedApp { public static void main(String[] args) {
OrderArray arr;
arr = new OrderArray(100);

arr.insert(22);
arr.insert(88);
arr.insert(55);
arr.insert(77);
arr.insert(11);
arr.insert(44);
arr.insert(00);
arr.insert(66);
arr.insert(99);
arr.insert(33);

arr.display();

long SearchKey = 11;
if(arr.find(SearchKey) == arr.size()){
System.out.println("Can't find " + SearchKey);
}
else{
System.out.println("Found " + SearchKey);
}

arr.delete(44);
arr.delete(00);

arr.display();
}}

解决方案 »

  1.   

    我自己找出了错误,呵呵!
    public OrderArray(int max){
            a = new long[max];
            nElem = max;//应该是nElem = 0;
        }
      

  2.   

    at OrderArray.insert(OrderedApp.java:43) 
    关键是这行的代码,你贴出来,大家就容易帮你查出问题
      

  3.   

    不知道你要干什么,貌似ArrayList功能,干吗不直接用ArrayList呢,你的insert实现的时候不对,需要考虑当前数组够不够存储,如果不够需要重新创建数组,然后把原来的数组copy过来,然后再把新的值插入。
      

  4.   

    public void insert(long value){
            int j;
            for(j = 0;j < nElem;j ++){
                if(a[j] > value){
                    break;
                }
            }
            for(int k = nElem;k > j;k --){
                a[k] = a[k - 1];
            }
            a[j] = value;
            nElem ++;
        }
    这里越界了。100 + 1 大于你初始化的长度了
      

  5.   

    insert 方法写的有问题, 再仔细检查一下
      

  6.   

    谢谢大家,find这里也有问题,把">"写成了"<"
    if(a[CurIn] < SearchKey){    //应该是>
                        UpperBound = CurIn - 1;
                    }
                    else{
                        LowerBound = CurIn + 1;
                    }
      

  7.   

    唉……
    逻辑上的问题。看这段代码:
             public void insert(long value) {
    int j;
    for (j = 0; j < nElem; j++) {
    if (a[j] > value) {
    break;
    }
    }
    for (int k = nElem; k > j; k--) {
    a[k] = a[k - 1];
    }
    a[j] = value;
    nElem++;
    }首先,a[]
    作为长整形类成员变量,在你初始化之后,默认使用0填充。
    分析一下你的第一次插入操作。以j作为循环控制变量,自然想要在找到一个大于你插入的22之后break出循环是不可能的,这样循环直道满足结束条件也就是 就是j < nElem;你的nElem传递的100,循环退出时j的值也是100。
    那么下面你那个以k做控制的循环也就根本不会进入。
    最要命的问题是你使用a[j],前面说了j这个时候等于100啊。作为100个长度的数组,下标的范围是0-99,你这里用了一个a[100]自然就出现了下标越界的问题。唉……你没有看出什么问题,可是,这段代码却是问题重重。当然,解决起来没那么费劲了,lz更换一下思路。
    呵呵,考虑控制,考虑边界情形。
      

  8.   

    谢谢,差不多是ArrayList功能,我初学JAVA,自己尝试写一些算法。
      

  9.   

    谢谢大家,程序调OK了,超出数组存储范围的情况暂不考虑在内,代码如下:class OrderArray{
    private long[] a;
    private int nElem;
    public OrderArray(int max){
    a = new long[max];
    nElem = 0;
    }
    public int size(){
    return nElem;
    }
    public int find(long SearchKey){
    int LowerBound = 0;
    int UpperBound = nElem - 1;
    int CurIn;
    while(true){
    CurIn = (LowerBound + UpperBound) / 2;
    if(a[CurIn] == SearchKey){
    return CurIn;
    }
    else if(LowerBound > UpperBound){
    return nElem;
    }
    else{
    if(a[CurIn] > SearchKey){
    UpperBound = CurIn - 1;
    }
    else{
    LowerBound = CurIn + 1;
    }
    }
    }
    }
    public void insert(long value){
    int j;
    if(nElem == 0){
    a[0] = value;
    nElem ++;
    }
    //nElem == max情况暂不予考虑
    else{
    for(j = 0;j < nElem;j ++){
    if(a[j] > value){
    break;
    }
    }
    for(int k = nElem;k > j;k --){
    a[k] = a[k - 1];
    }
    a[j] = value;
    nElem ++;
    }
    }
    public boolean delete(long value){
    int j;
    for(j = 0;j < nElem;j ++){
    if(a[j] == value){
    break;
    }
    }
    if(j == nElem){
    return false;
    }
    else{

    for(int k = j;k < nElem;k ++){
    a[k] = a[k + 1];
    }
    nElem --;
    return true;
    }
    }
    public void display(){
    for(int j = 0;j < nElem;j ++){
    System.out.print(a[j] + " ");
    }
    System.out.println("");
    }
    }
    public class OrderedApp { public static void main(String[] args) {
    OrderArray arr;
    arr = new OrderArray(100);

    arr.insert(22);
    arr.insert(88);
    arr.insert(55);
    arr.insert(77);
    arr.insert(11);
    arr.insert(44);
    arr.insert(00);
    arr.insert(66);
    arr.insert(99);
    arr.insert(33);

    arr.display();

    long SearchKey = 11;
    if(arr.find(SearchKey) == arr.size()){
    System.out.println("Can't find " + SearchKey);
    }
    else{
    System.out.println("Found " + SearchKey);
    }

    arr.delete(44);
    arr.delete(00);

    arr.display();
    }}