用J中的多维数组进行Arrays.fill
如何在不使用循环的情况下用Java填充多维数组? 我试过了:
double[][] arr = new double[20][4];
Arrays.fill(arr, 0);
结果为java.lang.ArrayStoreException: java.lang.Double
提前致谢!
11个解决方案
87 votes
这是因为new是1.0的数组,您无法将0.0分配给它(就像double[] vector = 0.0一样)。 实际上,Java没有真正的多维数组。
碰巧,new是Java中double的默认值,因此当从new中获取矩阵时,矩阵实际上已经被零填充了。但是,如果要用1.0进行填充,则可以执行以下操作:
我不认为API提供了无需循环即可解决此问题的方法。 然而,使用for-each循环非常简单。
double[][] matrix = new double[20][4];
// Fill each row with 1.0
for (double[] row: matrix)
Arrays.fill(row, 1.0);
aioobe answered 2020-01-11T05:42:20Z
60 votes
double[][] arr = new double[20][4];
Arrays.fill(arr[0], 0);
Arrays.fill(arr[1], 0);
Arrays.fill(arr[2], 0);
Arrays.fill(arr[3], 0);
Arrays.fill(arr[4], 0);
Arrays.fill(arr[5], 0);
Arrays.fill(arr[6], 0);
Arrays.fill(arr[7], 0);
Arrays.fill(arr[8], 0);
Arrays.fill(arr[9], 0);
Arrays.fill(arr[10], 0);
Arrays.fill(arr[11], 0);
Arrays.fill(arr[12], 0);
Arrays.fill(arr[13], 0);
Arrays.fill(arr[14], 0);
Arrays.fill(arr[15], 0);
Arrays.fill(arr[16], 0);
Arrays.fill(arr[17], 0);
Arrays.fill(arr[18], 0);
Arrays.fill(arr[19], 0);
trojanfoe answered 2020-01-11T05:42:35Z
9 votes
OP询问如何无循环解决此问题! 由于某些原因,如今避免循环很流行。 为什么是这样? 可能已经意识到,使用map、Function、eval和朋友,以及诸如exec之类的方法可以隐藏循环并减少程序的详细信息,并且很酷。 真正好的Unix管道也是如此。 或jQuery代码。 事情看起来很好,没有循环。
但是Java是否有map方法? 并非如此,但是我们可以使用eval或exec方法或eval接口定义一个带有Function接口的接口。 这不太困难,将是一个很好的练习。 它可能很昂贵,并且实际上没有使用。
没有循环的另一种方法是使用尾递归。 是的,这很愚蠢,也没有人会在实践中使用它,但是它确实表明,在这种情况下,循环很好。 尽管如此,仅为了展示“另一个免费循环示例”并获得乐趣,这里是:
import java.util.Arrays;
public class FillExample {
private static void fillRowsWithZeros(double[][] a, int rows, int cols) {
if (rows >= 0) {
double[] row = new double[cols];
Arrays.fill(row, 0.0);
a[rows] = row;
fillRowsWithZeros(a, rows - 1, cols);
}
}
public static void main(String[] args) {
double[][] arr = new double[20][4];
fillRowsWithZeros(arr, arr.length - 1, arr[0].length);
System.out.println(Arrays.deepToString(arr));
}
}
这不是很漂亮,但是在回答OP的问题时,没有明确的循环。
Ray Toal answered 2020-01-11T05:43:10Z
5 votes
根据Java 8,我们可以使用这种方式。
double[][] arr = new double[20][4];
Arrays.stream(arr).forEach(a -> Arrays.fill(a, 0));
我们可以用一种更好,更智能的方式在多维数组中初始化一个值。
Bipool answered 2020-01-11T05:43:38Z
4 votes
如何在不使用循环的情况下用Java填充多维数组?
多维数组只是数组的数组,Array不会检查数组的类型和您传递的值(此责任在开发人员身上)。
因此,如果不使用循环,就无法合理地填充多维数组。
请注意,与C或C ++之类的语言不同,Java数组是对象,而在多维数组中,除最后一级外,所有数组都包含对其他Array对象的引用。 我对此不是100%的确定,但是很可能它们是分布在内存中的,因此您不能只填充没有循环的连续块,就像C / C ++可以做到的那样。
Thomas answered 2020-01-11T05:44:13Z
3 votes
作为答案的扩展,我找到了这篇文章,但希望填充4维数组。原始示例只是一个二维数组,但问题是“多维”。 我不想为此发布新的问题...
您可以使用相同的方法,但是必须嵌套它们,以便最终获得一维数组。
fourDArray = new float[10][10][10][1];
// Fill each row with null
for (float[][][] row: fourDArray)
{
for (float[][] innerRow: row)
{
for (float[] innerInnerRow: innerRow)
{
Arrays.fill(innerInnerRow, -1000);
}
}
};
k3yz101 answered 2020-01-11T05:44:42Z
1 votes
有时我们所有人都不希望有一个
void java.util.Arrays.deepFill(T[]…multiDimensional).问题开始于
Object threeByThree[][] = new Object[3][3];
threeByThree[1] = null;和
threeByThree[2][1] = new int[]{42};完全合法。
(如果只有Object twoDim[]final[]是合法且定义明确的…)
(从下面使用一种公共方法可以使调用源代码保持循环。
如果您坚持不使用任何循环,请使用递归将循环和对Arrays.fill()(!)的调用替换。)
/** Fills matrix {@code m} with {@code value}.
* @return {@code m}'s dimensionality.
* @throws java.lang.ArrayStoreException if the component type
* of a subarray of non-zero length at the bottom level
* doesn't agree with {@code value}'s type. */
public static int deepFill(Object[] m, T value) {
Class> components;
if (null == m ||
null == (components = m.getClass().getComponentType()))
return 0;
int dim = 0;
do
dim++;
while (null != (components = components.getComponentType()));
filler((Object[][])m, value, dim);
return dim;
}
/** Fills matrix {@code m} with {@code value}.
* @throws java.lang.ArrayStoreException if the component type
* of a subarray of non-zero length at level {@code dimensions}
* doesn't agree with {@code value}'s type. */
public static void fill(Object[] m, T value, int dimensions) {
if (null != m)
filler(m, value, dimensions);
}
static void filler(Object[] m, T value, int toGo) {
if (--toGo <= 0)
java.util.Arrays.fill(m, value);
else
for (Object[] subArray : (Object[][])m)
if (null != subArray)
filler(subArray, value, toGo);
}
greybeard answered 2020-01-11T05:45:32Z
0 votes
public static Object[] fillArray(Object[] arr,Object item){
Arrays.fill(arr, item);
return arr;
}
Character[][] maze = new Character[10][10];
fillArray(maze, fillArray(maze[0], '?'));
for(int i = 0;i<10;i++){
System.out.println();
for(int j = 0;j<10;j++){
System.out.print(maze[i][j]);
}
}
我希望这做得好
Ahmed Mazher answered 2020-01-11T05:45:54Z
0 votes
使用Java 8,您可以声明和初始化二维数组,而无需使用(显式)循环,如下所示:
int x = 20; // first dimension
int y = 4; // second dimension
double[][] a = IntStream.range(0, x)
.mapToObj(i -> new double[y])
.toArray(i -> new double[x][]);
这将使用默认值初始化数组(在double的情况下为DoubleStream)。
如果要显式定义要使用的填充值,可以添加DoubleStream:
int x = 20; // first dimension
int y = 4; // second dimension
double v = 5.0; // fill value
double[][] a = IntStream
.range(0, x)
.mapToObj(i -> DoubleStream.generate(() -> v).limit(y).toArray())
.toArray(i -> new double[x][]);
Robby Cornelissen answered 2020-01-11T05:46:23Z
0 votes
简而言之,java不提供此类API。您需要遍历循环,并使用fill方法可以用一个循环填充2D数组。
int row = 5;
int col = 6;
int cache[][]=new int[row][col];
for(int i=0;i<=row;i++){
Arrays.fill(cache[i]);
}
Sanjeev Guglani answered 2020-01-11T05:46:43Z
-5 votes
Arrays.fill(arr, new double[4]);
watereater answered 2020-01-11T05:46:59Z