【问题标题】:How to obtain a part of a 2d array?如何获得二维数组的一部分?
【发布时间】:2014-12-07 14:30:08
【问题描述】:

假设我有一个像

这样的 4x4 数组矩阵
aaaa
abba
abba
aaaa

我可以从上面的矩阵中获得一个 3x3 矩阵并将其存储在另一个二维数组中吗? 3x3 矩阵应包含元素

aaa
abb
abb 

同样

aaa
bba
bba

还有另外两个矩阵。

这可以使用 Arrays.copyOfRange 来完成吗??

编辑:我还想要其他 2 个 3x3 矩阵,即。

abb
abb
aaa

bba
bba
aaa

就像我传递 2x2 矩阵中的元素一样,即 4x4 中的元素。 b's ,它应该给我一个围绕元素的 3x3 矩阵。

我可以通过使用一个 for 循环来实现这点b(上面给出)。但只是想知道是否有更简单的方法。

【问题讨论】:

  • 也许您可以先 (a) 阅读文档:docs.oracle.com/javase/7/docs/api/java/util/…,(b) 试一试。如果这对您不起作用,请发布您针对特定问题尝试过的代码。
  • 是的,我看到了文档。但是我可以发现 copyofrange 可以从一维数组中获取数据。我想知道它如何用于二维数组。我可以使用 4x4 矩阵中的中心 2x2 并获取围绕 2x2 的每个元素的元素,以使用 for 循环获得所需的 3x3 数组。只是想知道是否有简单的方法。
  • 如果您添加了更多关于您将如何处理这些子阵列的信息,可能会给出进一步的提示。例如,您可能会考虑一个interface Array2D { ... },它有一个方法Array2D getSubArray(...),它只返回子数组上的一个view。这里有很多解决方案,有些可能比复制更优雅,具体取决于应用案例。

标签: java arrays matrix dimensional


【解决方案1】:

我会使用 System.lang.arrayCopy:

import java.util.Arrays;

public class ArrayRange {

    public static void main(String[] args) {

        char[][] original = createMatrix(4);

        // copy 3x3 array starting at 1,0
        char[][] subArray = copySubrange(original, 1, 0, 3, 3);

        printArray(original);
        printArray(subArray);
    }

    private static char[][] copySubrange(char[][] source, int x, int y, int width, int height) {
        if (source == null) {
            return null;
        }
        if (source.length == 0) {
            return new char[0][0];
        }
        if (height < 0) {
            throw new IllegalArgumentException("height must be positive");
        }
        if (width < 0) {
            throw new IllegalArgumentException("width must be positive");
        }
        if ((y + height) > source.length) {
            throw new IllegalArgumentException("subrange too high");
        }
        char[][] dest = new char[height][width];
        for (int destY = 0; destY < height; destY++) {
            char[] srcRow = source[(y + destY)];
            if ((x + width) > srcRow.length) {
                throw new IllegalArgumentException("subrange too wide");
            }
            System.arraycopy(srcRow, x, dest[destY], 0, width);
        }
        return dest;
    }

    // Set up a matrix as an array of rows.
    // The y-coordinate is the position of a row in the array.
    // The x-coordinate is the position of an element in a row.
    private static char[][] createMatrix(int size) {
        char[][] original = new char[size][size];
        for (int y = 0; y < original.length; y++) {
            for (int x = 0; x < original[0].length; x++) {
                original[y][x] = (char) (Math.random() * 10 + 48);
            }
        }
        return original;
    }

    private static void printArray(char[][] array) {
        for (int y = 0; y < array.length; y++) {
            System.out.println(Arrays.toString(array[y]));
        }
        System.out.println();
    }
}

示例输出:

[0, 7, 4, 3]
[9, 2, 7, 2]
[9, 2, 4, 0]
[9, 1, 5, 9]

[7, 4, 3]
[2, 7, 2]
[2, 4, 0]

编辑:使用范围检查改进代码。

【讨论】:

  • 是的!这就是我需要的。非常感谢 :) 你提到的范围检查是为了避免数组越界异常对吗?
  • 正确,可以检查避免npe的
猜你喜欢
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 2020-04-27
  • 2022-10-05
相关资源
最近更新 更多