非常基本 - Java 数组作为类变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6183799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:44:13  来源:igfitidea点击:

Very basic - Java array as class variable

javaarrays

提问by animeman420

class Foo{
     int[] doop;

     public Foo(){
          this.doop={1,2,3,4,5};
     }
}

I can't compile this, Java ME SDK gives me a bunch of "Illegal Start of Expression" errors. Why? How do I make this work?

我无法编译这个,Java ME SDK 给了我一堆“非法的表达式开始”错误。为什么?我如何使这项工作?

回答by wjans

Try this:

试试这个:

this.doop= new int[]{1,2,3,4,5};

回答by Vladimir Ivanov

You can't do this in constructor, because this syntax is allowed only for declaration with initialization. Fix to this:

您不能在构造函数中执行此操作,因为此语法仅允许用于带有初始化的声明。修复这个:

class Foo{
     int[] doop = new int[]{1,2,3,4,5};

     public Foo(){

     }
}

回答by Joey

class Foo{
     int[] doop;

     public Foo(){
          this.doop= new int[]{1,2,3,4,5};
     }
}