java array 问题
时间:2013-11-17
来源:互联网
下面系条件 用one-dimensional array
Array name: dblScore
~Data type: double
~ Size: 10 elements
~ Values: un-initialized
我咁样写
public class Lab5 {
public static void main(String[] args) {
double[] array = new double [10];
System.out.println("Index/dblScore");
for( int i=0; i<array.length; i++)
System.out.println(i + "\t" + array );
}
}
但系output出
Index/dblScore
0 0.0
1 0.0
2 0.0
3 0.0
4 0.0
5 0.0
6 0.0
7 0.0
8 0.0
9 0.0
咁样咪唔符合Values: un-initialized
咁我应该点写??
Array name: dblScore
~Data type: double
~ Size: 10 elements
~ Values: un-initialized
我咁样写
public class Lab5 {
public static void main(String[] args) {
double[] array = new double [10];
System.out.println("Index/dblScore");
for( int i=0; i<array.length; i++)
System.out.println(i + "\t" + array );
}
}
但系output出
Index/dblScore
0 0.0
1 0.0
2 0.0
3 0.0
4 0.0
5 0.0
6 0.0
7 0.0
8 0.0
9 0.0
咁样咪唔符合Values: un-initialized
咁我应该点写??
作者: yob664 发布时间: 2013-11-17
唔明你点解未initialize都要print out佢...
Fine, 首先if你唔init佢at code level, Java compiler 系会initialize double 做 0 (depends on compiler,但多数都系0)如果你真系要睇到唔init同init左既分别(null& !null既分别),你应该用wrapper type: Double,而唔系double.
public class Lab5 {
public static void main(String[] args) {
Double[] array = new Double [10];
System.out.println("Index/dblScore");
for( int i=0; i<array.length; i++)
System.out.println(i + "\t" + array[ i ]);
}
}
Result:Index/dblScore
0 null
1 null
2 null
3 null
4 null
5 null
6 null
7 null
8 null
9 null
[ 本帖最后由 笨木 於 2013-10-29 10:02 AM 编辑 ]
Fine, 首先if你唔init佢at code level, Java compiler 系会initialize double 做 0 (depends on compiler,但多数都系0)如果你真系要睇到唔init同init左既分别(null& !null既分别),你应该用wrapper type: Double,而唔系double.
public class Lab5 {
public static void main(String[] args) {
Double[] array = new Double [10];
System.out.println("Index/dblScore");
for( int i=0; i<array.length; i++)
System.out.println(i + "\t" + array[ i ]);
}
}
Result:Index/dblScore
0 null
1 null
2 null
3 null
4 null
5 null
6 null
7 null
8 null
9 null
[ 本帖最后由 笨木 於 2013-10-29 10:02 AM 编辑 ]
作者: 笨木 发布时间: 2013-11-17
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28