在 Java 中扩展多维数组 (2D)

发布于 2024-10-06 11:27:22 字数 110 浏览 5 评论 0原文

如何在 Java 中扩展多维数组?

我需要扩展它的第一个维度。它的格式如下:
myArray[x][7]

“7”在所有扩展部分上将保持“7”。

How can I extend a multidimensional array in Java?

I need to extend its first dimension. It's in a format like:
myArray[x][7]

The "7" will stay "7" on all extended parts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔一刀 2024-10-13 11:27:22

Java 中的数组具有固定长度,因此您无法真正扩展它。您必须创建一个更大的新数组,然后将旧数组的内容复制到新数组中。像这样:

public class Test {

    public static void main(String[] args) {
        int[][] myArray = new int[3][7];

        // Print first dimension
        System.out.println(myArray.length);   // prints 3

        myArray = addRow(myArray);

        // Print first dimension
        System.out.println(myArray.length);   // prints 4

    }

    private static int[][] addRow(int[][] previous) {

        int prevRowCount = previous.length;

        int[][] withExtraRow = new int[prevRowCount + 1][];
        System.arraycopy(previous, 0, withExtraRow, 0, previous.length);
        withExtraRow[prevRowCount] = new int[] { 1, 2, 3, 4, 5, 6, 7 };

        return withExtraRow;
    }

}

您当然也可以使用动态增长的 ArrayList 等。 (这实际上是处理动态增长数组时的首选方法。)

import java.util.*;

public class Test {

    public static void main(String[] args) {
        List<int[]> myArray = new ArrayList<int[]>();

        // Print first dimension
        System.out.println(myArray.size());   // prints 0

        // Add three rows
        myArray.add(new int[] {  1,  2,  3,  4,  5,  6,  7 });
        myArray.add(new int[] { 11, 12, 13, 14, 15, 16, 17 });
        myArray.add(new int[] { 21, 22, 23, 24, 25, 26, 27 });

        // Print first dimension
        System.out.println(myArray.size());   // prints 3

    }
}

Arrays in Java have fixed length, so you can't really extend it. You will have to create a new, larger, array, and then copy the content of the old array into the new one. Like this:

public class Test {

    public static void main(String[] args) {
        int[][] myArray = new int[3][7];

        // Print first dimension
        System.out.println(myArray.length);   // prints 3

        myArray = addRow(myArray);

        // Print first dimension
        System.out.println(myArray.length);   // prints 4

    }

    private static int[][] addRow(int[][] previous) {

        int prevRowCount = previous.length;

        int[][] withExtraRow = new int[prevRowCount + 1][];
        System.arraycopy(previous, 0, withExtraRow, 0, previous.length);
        withExtraRow[prevRowCount] = new int[] { 1, 2, 3, 4, 5, 6, 7 };

        return withExtraRow;
    }

}

You could of course also use for instance an ArrayList<SomeType[]> that grows dynamically. (This is actually the preferred way when dealing with dynamically growing arrays.)

import java.util.*;

public class Test {

    public static void main(String[] args) {
        List<int[]> myArray = new ArrayList<int[]>();

        // Print first dimension
        System.out.println(myArray.size());   // prints 0

        // Add three rows
        myArray.add(new int[] {  1,  2,  3,  4,  5,  6,  7 });
        myArray.add(new int[] { 11, 12, 13, 14, 15, 16, 17 });
        myArray.add(new int[] { 21, 22, 23, 24, 25, 26, 27 });

        // Print first dimension
        System.out.println(myArray.size());   // prints 3

    }
}
花心好男孩 2024-10-13 11:27:22

您还可以使用 Arrays.copyOf 方法,例如:

import java.util.Arrays;

public class Array2D {
    public static void main(String[] args) {
        int x = 5 //For example
        int[][] myArray = [x][7];

        myArray = Arrays.copyOf(myArray, myArray.length + 1); //Extending for one
        myArray[myArray.length - 1] = new int[]{1, 2, 3, 4, 5, 6, 7};
    }
}

You could also use Arrays.copyOf method, for example:

import java.util.Arrays;

public class Array2D {
    public static void main(String[] args) {
        int x = 5 //For example
        int[][] myArray = [x][7];

        myArray = Arrays.copyOf(myArray, myArray.length + 1); //Extending for one
        myArray[myArray.length - 1] = new int[]{1, 2, 3, 4, 5, 6, 7};
    }
}
长安忆 2024-10-13 11:27:22

如果您想这样做,请创建一个包含数组作为实例变量并处理与其交互的类。

if you want to do this, make a class that contains the array as an instance variable and handles all interaction with it.

无言温柔 2024-10-13 11:27:22

你不能。

您可以创建一个具有正确尺寸的新数组,但我建议您阅读有关 ArrayList 类的信息,该类(据您需要了解)允许动态数组。

You can't.

You could create a new array with the correct dimensions, but I would recommend reading up about the ArrayList class which (as far as you need to know) allows for dynamic arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文