在 Java 中将三维数组的所有值设置为零的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/863507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:07:36  来源:igfitidea点击:

What's the best way to set all values of a three-dimensional array to zero in Java?

javaarraysmultidimensional-array

提问by Eric Wilson

I have a three-dimensional array that I want to reset to zero. It seems that there should be an easy way to do this that doesn't involve three forloops:

我有一个想要重置为零的三维数组。似乎应该有一种简单的方法来做到这一点,不涉及三个for循环:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
            cube[i][j][k] = 0;
        }
    }
}

回答by Yishai

If you are using JDK 1.5 or higher:

如果您使用的是 JDK 1.5 或更高版本:

    for (int[][] square : cube) {
        for (int[] line : square) {
            Arrays.fill(line, 0);
        }
    }

回答by Mnementh

An array will be filled with zeros after initialization:

初始化后,数组将用零填充:

int[][][] cube = new int[10][20][30];

This you can do also later, to reset them array to zero, it is not limited to declaration:

您也可以稍后执行此操作,将它们的数组重置为零,它不仅限于声明:

cube = new int[10][20][30];

Simply create a new array, it is initialized with zeros. This works if you have one place that is holding the reference to the array. Don't care about the old array, that will be garbage collected.

简单地创建一个新数组,它用零初始化。如果您有一个地方保存对数组的引用,则此方法有效。不要关心旧数组,那会被垃圾收集。

If you don't want to depend on this behavior of the language or you can't replace all occurrences of references to the old array, than you should go with the Arrays.fill()as jjnguy mentioned:

如果您不想依赖语言的这种行为,或者您不能替换所有对旧数组的引用,那么您应该使用jjnguy 提到的Arrays.fill()

for (int i = 0; i < cube.length; i++)
{
   for (int j = 0; j < cube[i].length; j++)
   {
      Arrays.fill(cube[i][j], 0);
   }
}

Arrays.fill seems to use a loop in the inside too, but it looks generally more elegant.

Arrays.fill 内部似乎也使用了一个循环,但总体上看起来更优雅。

回答by bruno conde

Well, it seems that you could just abandon the old array and create a new one:

好吧,看来您可以放弃旧数组并创建一个新数组:

int size = 10;
cube = new int[size][size][size];

回答by jjnguy

for (int i = 0; i < arr.length; i++){
    for (int j = 0; j < arr[i].length){
        Arrays.fill(arr[i][j], 0);
    }
}

That way you get rid of one extra loop using Arrays.fill;

这样你就可以使用Arrays.fill摆脱一个额外的循环;

Or

或者

arr = new double[arr.length][arr[0].length][arr[0][0].length];

Unfortunately, this assumes the array is at least length >= 1.

不幸的是,这假设数组至少为length >= 1

回答by paxdiablo

Despite the fact that that isa 3D array, the most readable solution is often the best (best is a subjective word, you should have said best in terms of some property, and readability is usually my first choice).

尽管这一个 3D 数组,但最易读的解决方案通常是最好的(最好是一个主观词,你应该在某些属性方面说最好,可读性通常是我的第一选择)。

If there were really only three elements in the inner loop and you wanted to emphasize that there were three columns, you could try this:

如果内部循环中确实只有三个元素,并且您想强调有三列,则可以尝试以下操作:

for (int i = 0; i < n; i++) { 
    for (int j = 0; j < n; j++) { 
        cube[i][j][0] = 0; 
        cube[i][j][1] = 0; 
        cube[i][j][2] = 0; 
    }
}

回答by paxdiablo

Well, you could always do this:

好吧,你总是可以这样做:

Arrays.fill(cube[0][0],0);
Arrays.fill(cube[0],cube[0][0]);
Arrays.fill(cube,cube[0]);

It's a little cleaner than 3 loops.

它比 3 个循环要干净一些。

If you don't get the idea, the first "fill" fills a single dimension. The second fill copies that one dimension across two dimension. The third fill copies the two dimensions across three dimensions.

如果您不明白,第一个“填充”将填充一个维度。第二个填充在二维上复制一维。第三次填充跨三个维度复制两个维度。

If you don't have other references to the array that you need to preserve, re-creating it as others have suggested is probably faster and cleaner.

如果您没有对需要保留的数组的其他引用,按照其他人的建议重新创建它可能会更快更干净。

回答by paxdiablo

Just create a new array and assign the variable to it... The GC will clean up the old array

只需创建一个新数组并将变量分配给它...... GC 将清理旧数组

回答by paxdiablo

If all the rows are the same length you could just discard the array and build a new one since the default value of int elements is zero.

如果所有行的长度相同,您可以丢弃数组并构建一个新数组,因为 int 元素的默认值为零。

cube = new int[cube.length][cube[0].length][cube[0][0].length];

Or you could do

或者你可以做

for(int[][] tda : cube ) {
  for(int[] oda : tda) {
    java.util.Arrays.fill(oda, 0);
  }
}