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 方法如下:
static void fill(boolean[] a, boolean val)
将指定的 boolean 值分配给指定 boolean 型数组的每个元素。
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
将指定的 boolean 值分配给指定 boolean 型数组指定范围中的每个元素。
static void fill(byte[] a, byte val)
将指定的 byte 值分配给指定 byte 节型数组的每个元素。
static void fill(byte[] a, int fromIndex, int toIndex, byte val)
将指定的 byte 值分配给指定 byte 型数组指定范围中的每个元素。
static void fill(char[] a, char val)
将指定的 char 值分配给指定 char 型数组的每个元素。
static void fill(char[] a, int fromIndex, int toIndex, char val)
将指定的 char 值分配给指定 char 型数组指定范围中的每个元素。
static void fill(double[] a, double val)
将指定的 double 值分配给指定 double 型数组的每个元素。
static void fill(double[] a, int fromIndex, int toIndex, double val)
将指定的 double 值分配给指定 double 型数组指定范围中的每个元素。
static void fill(float[] a, float val)
将指定的 float 值分配给指定 float 型数组的每个元素。
static void fill(float[] a, int fromIndex, int toIndex, float val)
将指定的 float 值分配给指定 float 型数组指定范围中的每个元素。
static void fill(int[] a, int val)
将指定的 int 值分配给指定 int 型数组的每个元素。
static void fill(int[] a, int fromIndex, int toIndex, int val)
将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
static void fill(long[] a, int fromIndex, int toIndex, long val)
将指定的 long 值分配给指定 long 型数组指定范围中的每个元素。
static void fill(long[] a, long val)
将指定的 long 值分配给指定 long 型数组的每个元素。
static void fill(Object[] a, int fromIndex, int toIndex, Object val)
将指定的 Object 引用分配给指定 Object 数组指定范围中的每个元素。
static void fill(Object[] a, Object val)
将指定的 Object 引用分配给指定 Object 数组的每个元素。
static void fill(short[] a, int fromIndex, int toIndex, short val)
将指定的 short 值分配给指定 short 型数组指定范围中的每个元素。
static void fill(short[] a, short val)
将指定的 short 值分配给指定 short 型数组的每个元素。
将指定的 boolean 值分配给指定 boolean 型数组的每个元素。
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
将指定的 boolean 值分配给指定 boolean 型数组指定范围中的每个元素。
static void fill(byte[] a, byte val)
将指定的 byte 值分配给指定 byte 节型数组的每个元素。
static void fill(byte[] a, int fromIndex, int toIndex, byte val)
将指定的 byte 值分配给指定 byte 型数组指定范围中的每个元素。
static void fill(char[] a, char val)
将指定的 char 值分配给指定 char 型数组的每个元素。
static void fill(char[] a, int fromIndex, int toIndex, char val)
将指定的 char 值分配给指定 char 型数组指定范围中的每个元素。
static void fill(double[] a, double val)
将指定的 double 值分配给指定 double 型数组的每个元素。
static void fill(double[] a, int fromIndex, int toIndex, double val)
将指定的 double 值分配给指定 double 型数组指定范围中的每个元素。
static void fill(float[] a, float val)
将指定的 float 值分配给指定 float 型数组的每个元素。
static void fill(float[] a, int fromIndex, int toIndex, float val)
将指定的 float 值分配给指定 float 型数组指定范围中的每个元素。
static void fill(int[] a, int val)
将指定的 int 值分配给指定 int 型数组的每个元素。
static void fill(int[] a, int fromIndex, int toIndex, int val)
将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
static void fill(long[] a, int fromIndex, int toIndex, long val)
将指定的 long 值分配给指定 long 型数组指定范围中的每个元素。
static void fill(long[] a, long val)
将指定的 long 值分配给指定 long 型数组的每个元素。
static void fill(Object[] a, int fromIndex, int toIndex, Object val)
将指定的 Object 引用分配给指定 Object 数组指定范围中的每个元素。
static void fill(Object[] a, Object val)
将指定的 Object 引用分配给指定 Object 数组的每个元素。
static void fill(short[] a, int fromIndex, int toIndex, short val)
将指定的 short 值分配给指定 short 型数组指定范围中的每个元素。
static void fill(short[] a, short val)
将指定的 short 值分配给指定 short 型数组的每个元素。
转:http://blog.youkuaiyun.com/alenc/article/details/1500113