A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈查理 中级黑马   /  2014-7-17 00:30  /  796 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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;
                                }
                        }
                }
        }
}

为什么运行报错

点评

把报错的提示写上去,这个能让别人能好的回答你的问题  发表于 2014-7-17 07:45

2 个回复

正序浏览
本帖最后由 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.                
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马