Java 中的 Arrays.fill 多维数组
如何在不使用循环的情况下在 Java 中填充多维数组?我尝试过:
double[][] arr = new double[20][4];
Arrays.fill(arr, 0);
这会导致 java.lang.ArrayStoreException: java.lang.Double
How can I fill a multidimensional array in Java without using a loop? I've tried:
double[][] arr = new double[20][4];
Arrays.fill(arr, 0);
This results in java.lang.ArrayStoreException: java.lang.Double
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这是因为
double[][]
是一个double[]
数组,您不能将0.0
分配给它(就像执行double[]向量= 0.0
)。事实上,Java没有真正的多维数组。碰巧,
0.0
是 Java 中双精度数的默认值,因此当您从new
。但是,如果您想用1.0
填充它,您可以执行以下操作:我不相信 API 提供了一种不使用循环即可解决此问题的方法。不过,使用 for-each 循环就足够简单了。
This is because a
double[][]
is an array ofdouble[]
which you can't assign0.0
to (it would be like doingdouble[] vector = 0.0
). In fact, Java has no true multidimensional arrays.As it happens,
0.0
is the default value for doubles in Java, thus the matrix will actually already be filled with zeros when you get it fromnew
. However, if you wanted to fill it with, say,1.0
you could do the following:I don't believe the API provides a method to solve this without using a loop. It's simple enough however to do it with a for-each loop.
按照 Java 8,我们可以使用这种方式。
我们可以用更好更智能的方式初始化多维数组中的值。
As Per Java 8, we can use this way.
We can initialize a value in multidimensional array in a nicer and smart way.
OP询问如何解决这个问题没有循环!出于某种原因,现在避免循环很流行。这是为什么呢?可能已经意识到使用
map
、reduce
、filter
和朋友以及each
等方法隐藏循环减少程序中的废话,这很酷。这同样适用于真正甜蜜的 Unix 管道。或者 jQuery 代码。没有循环,事情看起来就很棒。但是Java有
map
方法吗?并非如此,但我们可以使用带有eval
或exec
方法的Function
接口来定义一个。这并不太难,将是一个很好的练习。它可能很昂贵并且没有在实践中使用。另一种不使用循环的方法是使用尾递归。是的,这有点愚蠢,也没有人会在实践中使用它,但它确实表明,也许循环在这种情况下很好。尽管如此,只是为了展示“又一个无循环示例”并享受乐趣,这里是:
它并不漂亮,但为了回答OP的问题,没有显式循环。
The OP asked how to solve this problem without a loop! For some reason it is fashionable these days to avoid loops. Why is this? Probably there is a realization that using
map
,reduce
,filter
, and friends, and methods likeeach
hide loops and cut down on program verbage and are kind of cool. The same goes for really sweet Unix pipelines. Or jQuery code. Things just look great without loops.But does Java have a
map
method? Not really, but we could define one with aFunction
interface with aneval
orexec
method. It isn't too hard and would be a good exercise. It might be expensive and not used in practice.Another way to do this without a loop is to use tail recursion. Yes, it is kind of silly and no one would use it in practice either, but it does show, maybe, that loops are fine in this case. Nevertheless, just to show "yet another loop free example" and to have fun, here is:
It isn't pretty, but in answer to the OP's question, there are no explicit loops.
多维数组只是数组的数组,
fill(...)
不会检查数组的类型和您传入的值(此责任由开发人员承担)。因此,如果不使用循环,就无法很好地填充多维数组。
请注意,与 C 或 C++ 等语言不同,Java 数组是对象,并且在多维数组中,除了最后一层之外,所有数组都包含对其他 Array 对象的引用。我对此不是 100% 确定,但很可能它们分布在内存中,因此您不能只填充一个没有循环的连续块,就像 C/C++ 允许您这样做的那样。
Multidimensional arrays are just arrays of arrays and
fill(...)
doesn't check the type of the array and the value you pass in (this responsibility is upon the developer).Thus you can't fill a multidimensional array reasonably well without using a loop.
Be aware of the fact that, unlike languages like C or C++, Java arrays are objects and in multidimensional arrays all but the last level contain references to other
Array
objects. I'm not 100% sure about this, but most likely they are distributed in memory, thus you can't just fill a contiguous block without a loop, like C/C++ would allow you to do.作为答案的扩展,我找到了这篇文章,但希望填充一个 4 维数组。
原来的例子只是一个二维数组,但问题说“多维”。我不想为此发布一个新问题...
您可以使用相同的方法,但是您必须嵌套它们,以便最终得到一个一维数组。
As an extension to the answer, I found this post but was looking to fill a 4 dimensional array.
The original example is only a two dimensional array, but the question says "multidimensional". I didn't want to post a new question for this...
You can use the same method, but you have to nest them so that you eventually get to a single dimensional array.
Arrays.fill 适用于一维数组,因此要填充二维数组,我们可以执行以下操作
Arrays.fill works with single dimensional array, so to fill two dimensional array we can do below
我们有时不都希望有一个
void java.util.Arrays.deepFill(T[]…multiDimensional)
。问题始于对象三ByThree[][] = new Object[3][3];
twoByThree[1] = null;
和twoByThree[2][1] = new int[]{42};
完全合法。(如果只有
ObjecttwoDim[]final[]
是合法且定义良好的......)(使用下面的公共方法之一可以避免调用源代码中的循环。
如果您坚持根本不使用循环,请使用递归替换循环和对 Arrays.fill() 的调用(!)。)
Don't we all sometimes wish there was a
<T>void java.util.Arrays.deepFill(T[]…multiDimensional)
. Problems start withObject threeByThree[][] = new Object[3][3];
threeByThree[1] = null;
andthreeByThree[2][1] = new int[]{42};
being perfectly legal.(If only
Object twoDim[]final[]
was legal and well defined…)(Using one of the public methods from below keeps loops from the calling source code.
If you insist on using no loops at all, substitute the loops and the call to
Arrays.fill()
(!) using recursion.)使用 Java 8,您可以声明并初始化二维数组,而无需使用(显式)循环,如下所示:
这将使用默认值(在
double< 的情况下为
0.0
)初始化数组。 /代码>)。如果您想显式定义要使用的填充值,您可以添加
DoubleStream
:Using Java 8, you can declare and initialize a two-dimensional array without using a (explicit) loop as follows:
This will initialize the arrays with default values (
0.0
in the case ofdouble
).In case you want to explicitly define the fill value to be used, You can add in a
DoubleStream
:简单来说java不提供这样的API。
您需要遍历循环,并使用 fill 方法可以用一个循环填充 2D 数组。
In simple words java donot provide such an API.
You need to iterate through loop and using fill method you can fill 2D array with one loop.
Arrays.fill 仅适用于一维数组
java.util.Arrays 的来源:
使用自己的循环初始化数组
Arrays.fill works only with one-dimensional array
Source of java.util.Arrays:
Use own loops for initialization array
我希望这一切顺利
i hope this do well
递归解决方案
一个简单的递归解决方案,用任何给定值填充二维数组的每一行。
Recursive Solution
A simple Recursive Solution to fill every row of a 2d array with any given value.