| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 572 人关注过本帖
标题:请教高手帮忙看看程序输出有问题
取消只看楼主 加入收藏
零下零度
Rank: 1
等 级:新手上路
帖 子:60
专家分:5
注 册:2009-3-19
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
请教高手帮忙看看程序输出有问题
[free]public class TestBubbleSort {
    public static void main(String[] args) {
        BubbleArray ab = new BubbleArray(10);
        ab.add(20);
        ab.add(10);
        ab.add(33);
        ab.add(44);
        ab.add(99);
        ab.add(24);
        ab.add(11);
        ab.add(55);
        ab.add(88);    
        ab.add(22);
        
        ab.display();
        ab.insertSort();        
        ab.display();
        ab.delete(10);
        ab.display();
    }
}

class BubbleArray {
    private long[] a;
    private int nElems;
    
    BubbleArray(int max){
        a = new long[max];
        nElems = 0;
    }
    
    public void add(long value) {
        a[nElems] = value;
        nElems ++;
    }
    
    public boolean find(long value) {
        int i;
        for(i = 0; i < nElems; i++) {
            if(a[i] == value) {
                break;
            }
        }
        if(i == nElems) {
            return false;
        } else {
            return true;
        }
    
    }

    public void delete(long value){
        if(nElems == 0) {
            System.out.println("数组中已经没有元素了!");
        } else {
            for(int i = 0; i < nElems; i++) {
                if(a[i] == value) {
                    for(int j = i; j < nElems; j++) {
                        a[j] = a[j+1];
                    }
                                        
                }
                nElems--;                
            }    
        }
    }
/*//泡沫排序法
    public void bubbleSort() {
        long temp = 0;
        for(int i = nElems - 1; i > 1; i--) {  //排序的趟数
            for(int j = 0; j < i; j++){          //j<i:i后面的数已经排好序了,不需再比较
                if(a[j] > a[j +1]) {
                    swap(j, j + 1);
                    
                }
            }
        }
    }
*/ //交换算法    
  public void swap(int x, int y){
      long temp = a[x];
      a[x] = a[y];
      a[y] = temp;
  }
/* //选择排序法
    public void selectionSort(){
        for(int i = 0; i < nElems - 1; i++) {
            int min = i;                           
            for(int j = min + 1; j < nElems; j++) {
                if(a[j] < a[min]) {
                    min = j;                              //交换下标
                }                
            }
            swap(min, i);
        }
    }
*/
    //插入排序
    public void insertSort() {
        long temp;
        
        for(int i = 1; i < nElems; i++) {
            temp = a[i];
            int j = i;
            while(j > 0 && a[j - 1] >= temp) {
                a[j] = a[j - 1];
                --j;
            }
            a[j] = temp;
        }
    }
    
    public void display() {
        for(int i = 0; i < nElems; i++) {
            System.out.print(a[i] + "\t");
        }
        System.out.println();
    }
}[/free]
输出异常:
当ab.delete(10)时
20      10      33      44      99      24      11      55      88      22
10      11      20      22      24      33      44      55      88      99
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
        at BubbleArray.delete(TestBubbleSort.java:59)
        at TestBubbleSort.main(TestBubbleSort.java:18)


当ab.delete(除10之外的数)时 比如ab.delete(11)时输出为:
20      10      33      44      99      24      11      55      88      22
10      11      20      22      24      33      44      55      88      99
10      20      22      24      33  ( 其他数也只显示五个)
请教高手:为什么会出现以上两种情况呢,是不是delete() 方法不对呀,麻烦高手帮帮忙,不甚感激!!!

[[it] 本帖最后由 零下零度 于 2009-8-5 19:38 编辑 [/it]]
搜索更多相关主题的帖子: 输出 
2009-08-05 19:37
零下零度
Rank: 1
等 级:新手上路
帖 子:60
专家分:5
注 册:2009-3-19
收藏
得分:0 
回复 2楼 usbboy2009

这个问题我已经想过了,我也试过了,还是有问题!
2009-08-07 10:43
零下零度
Rank: 1
等 级:新手上路
帖 子:60
专家分:5
注 册:2009-3-19
收藏
得分:0 
明显当删除10的时候抛出异常,而删除其他数时能删除,但是多删了四个数???
2009-08-07 10:45
零下零度
Rank: 1
等 级:新手上路
帖 子:60
专家分:5
注 册:2009-3-19
收藏
得分:0 
回复 6楼 usbboy2009

谢谢,懂了!
不过我还是觉得nElems-- 放到if里面比较好
2009-08-12 10:50
快速回复:请教高手帮忙看看程序输出有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012299 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved