【问题标题】:Why Array initializer is not Allowed here? What is the problem [duplicate]为什么这里不允许使用数组初始化器?有什么问题[重复]
【发布时间】:2020-02-08 20:36:51
【问题描述】:
class Main {
    public static void main() {

        int[] array = new int[5] {1,2,3,4,5};

    }
}

【问题讨论】:

  • 你基本上做了两次。尝试 int[] 数组 = {1,2,3,4,5};
  • 在使用值进行初始化时,您不应该提供数组大小。
  • 如果您在 Eclipse 中检查错误,它会直接显示“提供数组初始值设定项时无法定义维度表达式”。初始化它没有大小。

标签: java arrays initializer


【解决方案1】:
    public static void main(String[] args) {


            int[] array = new int[] {1,2,3,4,5};

        }
}

您当前正在声明数组的大小两次,以上将起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多