java.util.Arrays.fill() 一个填充数组的方法:
例:有五个班级,每个班级有40个人,用 60 ~ 160 之间的数值来填充这些学生的体重。






开始时把它写成了






结果一直显示 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)(100*Math.random() + 60) 产生的随机值填充了整个数组,即 weight[0] = weight[1] = weight[2] = weight[3] = …… = weight[9] = 同一个值,而非我所想要的数组中每个项都以一个随机值填充,所以最后还是改用了最原始也是最有效的方法:







最后得出结论:java.util.Arrays.fill() 方法只适合于把数组用同一个值初始化。
如:


表示用 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型数组的每个元素。
将指定的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