黑马程序员技术交流社区

标题: 键盘录入之排序 [打印本页]

作者: 陈查理    时间: 2014-7-17 00:30
标题: 键盘录入之排序
public class MaoPao2 {

        public static void main(String[] args){
                Scanner s = new Scanner(System.in);
                System.out.println("请输入:");
               
                Integer st = s.nextInt();
                Integer[] list = new Integer[st];
               
                De d = new De();
                d.mp(list);
                for(int i=0; i<list.length; i++){
                        System.out.println(list[i]+" ");
                }
               
        }
       
}

class De{
        public static void mp(Integer[] arr){
                for(int x=0; x<arr.length-1; x++){
                        for(int y=0; y<arr.length -x -1; y++){
                                if(arr[y]<arr[y+1]){
                                        int temp = arr[y+1];
                                        arr[y+1] = arr[y];
                                        arr[y] = temp;
                                }
                        }
                }
        }
}

为什么运行报错
作者: icris    时间: 2014-7-17 00:43
本帖最后由 icris 于 2014-7-17 01:23 编辑

如果没有看错的话,这个 list 里面没有数据,都是 null 。
加上初始化应该就行了
  1. Integer[] list = new Integer[st];
  2. Random r = new Random();
  3. for(int i=0; i<list.length; i++){
  4.         list[i] = r.nextInt(1000);
  5. }
  6.                
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2