java.util.Arrays.fill()的使用概谈

本文探讨了java.util.Arrays.fill()方法的正确使用方式及其局限性,通过实例解释如何为数组元素赋值,指出了该方法仅适用于用单一值初始化数组的特点,并提供了替代方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.util.Arrays.fill() 一个填充数组的方法:

例:有五个班级,每个班级有40个人,用 60 ~ 160 之间的数值来填充这些学生的体重。
float[][]weight=newfloat[5][40];
for(inti=0;i<weight.length;i++){
for(intj=0;j<weight[i].length;j++){
java.util.Arrays.fill(weight[i],(
float)(100*Math.random()+60));
}

}
开始时把它写成了
float[][]weight=newfloat[5][40];
for(inti=0;i<weight.length;i++){
for(intj=0;j<weight[i].length;j++){
java.util.Arrays.fill(weight[i][j],(
float)(100*Math.random()+60));
}

}
结果一直显示 java.util.Arrays.fill(weight[i][j],(float)(100*Math.random() + 60)); 有错,查了下 API 才知道是用错了 fill() 方法,
因为 fill() 方法中没有 fill( float, float) 的方法,只有fill( float[], float) 方法
发现遇到问题后直接查 Java API 真的很有效,很快就能发现错误的原因,还能知道和这个知识点相关的信息,避免下次再犯类似的错误。
可是运行时又发现了一个问题,当然不是语法问题了,而是用 java.util.Arrays.fill( T[], T) 方法填充数组时,是整个数组都以一个值填充,就是说
Float[]weight=newfloat[10];
java.util.Arrays.fill(weight,(
float)(100*Math.random()+60));
其实只是以一个用 (float)(100*Math.random() + 60) 产生的随机值填充了整个数组,即 weight[0] = weight[1] = weight[2] = weight[3] = …… = weight[9] = 同一个值,而非我所想要的数组中每个项都以一个随机值填充,所以最后还是改用了最原始也是最有效的方法:
float[][]weight=newfloat[4][20];
for(inti=0;i<weight.length;i++){
for(intj=0;j<weight[i].length;j++){
//Arrays.fill(weight[i],(float)(100*(Math.random()+1)));
weight[i][j]=(float)(100*(Math.random()+1));
}

}
最后得出结论:java.util.Arrays.fill() 方法只适合于把数组用同一个值初始化。
如:
float[]height=newfloat[20];
java.util.Arrays.fill(height,
175.5f);
表示用 float 型的值 175.5f 初始化数组 height[20] ,即用此值填充所有数组项。
另外,还发现两个小东东:
JDK 1.4 之前的版本好像不支持 collection-based for 循环和 assert 断言
在 API 中所有的 fill 方法如下:
staticvoidfill(boolean[]a,booleanval)
将指定的
boolean值分配给指定boolean型数组的每个元素。
staticvoidfill(boolean[]a,intfromIndex,inttoIndex,booleanval)
将指定的
boolean值分配给指定boolean型数组指定范围中的每个元素。
staticvoidfill(byte[]a,byteval)
将指定的
byte值分配给指定byte节型数组的每个元素。
staticvoidfill(byte[]a,intfromIndex,inttoIndex,byteval)
将指定的
byte值分配给指定byte型数组指定范围中的每个元素。
staticvoidfill(char[]a,charval)
将指定的
char值分配给指定char型数组的每个元素。
staticvoidfill(char[]a,intfromIndex,inttoIndex,charval)
将指定的
char值分配给指定char型数组指定范围中的每个元素。
staticvoidfill(double[]a,doubleval)
将指定的
double值分配给指定double型数组的每个元素。
staticvoidfill(double[]a,intfromIndex,inttoIndex,doubleval)
将指定的
double值分配给指定double型数组指定范围中的每个元素。
staticvoidfill(float[]a,floatval)
将指定的
float值分配给指定float型数组的每个元素。
staticvoidfill(float[]a,intfromIndex,inttoIndex,floatval)
将指定的
float值分配给指定float型数组指定范围中的每个元素。
staticvoidfill(int[]a,intval)
将指定的
int值分配给指定int型数组的每个元素。
staticvoidfill(int[]a,intfromIndex,inttoIndex,intval)
将指定的
int值分配给指定int型数组指定范围中的每个元素。
staticvoidfill(long[]a,intfromIndex,inttoIndex,longval)
将指定的
long值分配给指定long型数组指定范围中的每个元素。
staticvoidfill(long[]a,longval)
将指定的
long值分配给指定long型数组的每个元素。
staticvoidfill(Object[]a,intfromIndex,inttoIndex,Objectval)
将指定的Object引用分配给指定Object数组指定范围中的每个元素。
staticvoidfill(Object[]a,Objectval)
将指定的Object引用分配给指定Object数组的每个元素。
staticvoidfill(short[]a,intfromIndex,inttoIndex,shortval)
将指定的
short值分配给指定short型数组指定范围中的每个元素。
staticvoidfill(short[]a,shortval)
将指定的
short值分配给指定short型数组的每个元素。

转:http://blog.youkuaiyun.com/alenc/article/details/1500113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值