
怎样用java定义一个int数组
1个回答
展开全部
int[] anArray = null;/*** 这样赋值在java中是不允许的,会判定为语法错误
* 首先java只可以在class中【声明】成员变量,而不能对变量赋值,
* 你只能在方法中(例如:main方法或者其他方法)对变量赋值。** 但是class中允许在声明变量的同时,为变量赋【初始值】。
* 也就是说 int[] anArray2 = new int[10]; 这样是允许的
* 这里的 new int[10] 是初始化操作(不是赋值操作)。
* *///anArray = new int[10];
static int[] bArray = null;
public void setValue(){
anArray = new int[10]; //这里是可以的,方法内可以赋值。}public static void main(String[] args){
//anArray = new int[10];/*** 这里是不允许的,因为主方法(即main方法)是静态的(由static修饰)
* 所以如果是静态的成员变量可以赋值,例如 bArray
* 下面的两种方式都是可以赋值的* */bArray = new int[10];
bArray = new int[]{1,2,2,1,1,1,1,1,1,1};}}<b答案补充</b
static修饰的方法,只可以调用static修饰的成员变量
所以在main方法中为【非静态】的anArray数组赋值是不允许的。
另外bArray = new int[]{1,2,2,1,1,1,1,1,1,1};是为数组赋值的方式。
* 首先java只可以在class中【声明】成员变量,而不能对变量赋值,
* 你只能在方法中(例如:main方法或者其他方法)对变量赋值。** 但是class中允许在声明变量的同时,为变量赋【初始值】。
* 也就是说 int[] anArray2 = new int[10]; 这样是允许的
* 这里的 new int[10] 是初始化操作(不是赋值操作)。
* *///anArray = new int[10];
static int[] bArray = null;
public void setValue(){
anArray = new int[10]; //这里是可以的,方法内可以赋值。}public static void main(String[] args){
//anArray = new int[10];/*** 这里是不允许的,因为主方法(即main方法)是静态的(由static修饰)
* 所以如果是静态的成员变量可以赋值,例如 bArray
* 下面的两种方式都是可以赋值的* */bArray = new int[10];
bArray = new int[]{1,2,2,1,1,1,1,1,1,1};}}<b答案补充</b
static修饰的方法,只可以调用static修饰的成员变量
所以在main方法中为【非静态】的anArray数组赋值是不允许的。
另外bArray = new int[]{1,2,2,1,1,1,1,1,1,1};是为数组赋值的方式。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询