java 数组的创建与注意事项

来源:互联网 发布:js人民币小写转大写 编辑:程序博客网 时间:2024/04/29 06:03

搬家后的博客链接: IT客栈 www.itkezhan.org


两种创建办法:


1、 int[] test ={1,2,3,4,5}  //创建数组的时候顺便初始化

2、 int[] test = new int[5] 或者int test[] = new int[5]   //建议采用第一种。



注意事项:


如果利用第二种办法创建了数组,则要给数组赋值的时候,必须要在方法体中。


class test{

int[]  test = new int[5];

test[0] = 1;    //没有在方法体中,所以会报错。


public void t()

{

int[0] = 1;  //在方法体中,所以不会报错。

}


}


原创粉丝点击