java.util.Arrays.fill()的使用概谈
来源:互联网 发布:企业出口海关数据 编辑:程序博客网 时间:2024/06/03 14:52
java.util.Arrays.fill() 一个填充数组的方法:
例:有五个班级,每个班级有40个人,用 60 ~ 160 之间的数值来填充这些学生的体重。
float[][] weight = new float[5][40];
for(int i = 0; i < weight.length; i++) {
for(int j = 0; j < weight[i].length; j++) {
java.util.Arrays.fill(weight[i],(float)(100*Math.random() + 60));
}
}
for(int i = 0; i < weight.length; i++) {
for(int j = 0; j < weight[i].length; j++) {
java.util.Arrays.fill(weight[i],(float)(100*Math.random() + 60));
}
}
开始时把它写成了
float[][] weight = new float[5][40];
for(int i = 0; i < weight.length; i++) {
for(int j = 0; j < weight[i].length; j++) {
java.util.Arrays.fill(weight[i][j],(float)(100*Math.random() + 60));
}
}
for(int i = 0; i < weight.length; i++) {
for(int j = 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 = new float[10];
java.util.Arrays.fill( weight, (float)(100*Math.random() + 60));
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= new float[4][20];
for(int i=0; i<weight.length; i++) {
for(int j=0; j<weight[i].length; j++) {
// Arrays.fill(weight[i],(float)(100*(Math.random()+1)));
weight[i][j] = (float)(100*(Math.random()+1));
}
}
for(int i=0; i<weight.length; i++) {
for(int j=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 = new float[20];
java.util.Arrays.fill( height, 175.5f );
java.util.Arrays.fill( height, 175.5f );
表示用 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.csdn.net/alenc/article/details/1500113
- java.util.Arrays.fill()的使用概谈
- java.util.Arrays.fill()的使用概谈
- java.util.Arrays的Fill方法
- android Arrays.fill()的使用
- java.util.Arrays使用例子
- java.util.Arrays.asList使用体会
- java.util.Arrays.asList使用体会
- java.util.Arrays之后,使用Arrays.copyOfRange方法
- java中数组的初始化(Arrays.fill())
- java.util.Arrays.asList 的小问题
- java.util.Arrays的排序分享
- java.util.Arrays的排序研究
- java.util.Arrays.asList 的小问题
- java.util.Arrays类
- java.util.Arrays类
- java.util.Arrays类
- java.util.Arrays
- java.util —Arrays
- Linux下学习动态库的笔记3
- NX Nastran 节点遍历的方法/FEA Nodes iteration in NX Nastran by NXOpen
- zed graph使用经验
- Fixing the Date Format Issue in Azure Reporting CTP
- thrift简介
- java.util.Arrays.fill()的使用概谈
- 千万级并发HAproxy均衡负载系统介绍
- 使用SurfaceView开发遇到的一些问题
- 黑马程序员——Java API
- 浅谈C++容器
- c#调用c++dll共享内存需要函数
- 招人不难
- RTP学习笔记(四)
- Android_Broadcast