String colorList[] = {"red", "blue", "green", "yellow"};
String colorList1[4] = {"red", "blue", "green", "yellow"};//编译会出错
如上代码所示,在colorList1在编译时就出错,具体原因是问什么那?

解决方案 »

  1.   

    在创建数组的时候,如果要把  声明,分配空间,赋值 写在一句里,colorList1[] 里是不能写数字的。或者写成 String colorList1[] = new String[4];
      

  2.   

    String colorList1[4] = {"red", "blue", "green", "yellow"};//编译会出错
    因为数组的元素个数已经由后面大括号的内容确定下来了,没有必要再写个4,实在是画蛇添足,还会造成麻烦:假如你写的不是4而是5呢?!
      

  3.   

    初始化数组只有两种方法:
    1.给元素赋初值 String colorList[] = {"red", "blue", "green", "yellow"};
    2.用new完成    String colorList[]=new String[number];
      

  4.   

    晕啊,因为你语法写错了,java没这么写的,数组是个对象,int []这是声明一个对象的类型,这个对象就叫“int []”你写成“int [4]”编译器怎么会认识
      

  5.   

    创建数组方法:
    1.给元素赋初值 String colorList[] = {"red", "blue", "green", "yellow"};
    2.用new完成 String colorList[]=new String[4];
    3. 创建并赋值  String colorList[]=new String[]{"red", "blue", "green", "yellow"};