为什么我的多维数组初始化不起作用?

huangapple go评论72阅读模式
英文:

Why are my multi-dimensional array initializations not working?

问题

由于某种原因,在IntelliJ中(如果这很重要),当我尝试初始化我的2D数组时,只有我指定大小的第一个框被初始化。也就是说:

int[][] grid = new int[9][9];

并且当我通过调试器运行时,它显示我已经创建了一个int[9][]的数组。有人知道我做错了什么吗?是不是我需要更新什么?任何帮助都很感谢。谢谢。

英文:

For some reason, in IntelliJ (if that matters), when I try to initialize my 2D arrays, only the first box gets initialized for the size that I am specifying. i.e.
int[][] grid = new int[9][9];
and when I run through with the debugger, it shows that I've created an array that is int[9][]. Does anyone know what I am doing wrong? could it be that I need to update something? anything helps. Thank you.

答案1

得分: 3

这只是一个调试器的表示。

它总是正常工作。
它确实在内存中创建了一个二维数组。

public static void main(String[] args) {
    int[][] grid = new int[9][9];

    System.out.println(grid.length);
    System.out.println(grid[0].length);
}

这段代码将始终正确返回维度:

9
9

在调试器中可能会得到类似这样的结果 - 明显没有写成 int[9][9],这不是您预期的内容,但这只是一种表示 - 没有关于正确性的问题。

为什么我的多维数组初始化不起作用?

英文:

It is just a debugger representation.

It always works fine.<br>
It do create a two dimensional array in memory.

public static void main(String[] args) {
    int[][] grid = new int[9][9];

    System.out.println(grid.length);
    System.out.println(grid[0].length);
}

This code will always return dimensions properly as:

9
9

What you might get in a debugger is something like this - where definitely is not written int[9][9] what you are expected, however this is a representation only - there is nothing with correctness.
为什么我的多维数组初始化不起作用?

答案2

得分: 0

我曾经遇到过完全相同的问题,并且我发现@Misho Zhghenti的回答是最好的!是的,IntelliJ在调试中的表示会导致混淆。在Java中,多维数组并不真的是那样,它只是一个数组的数组。

谢谢@EthanB21 @Misho Zhghenti

英文:

I've had exactly the same problem and I've found @Misho Zhghenti's answer is the best! Yes, IntelliJ's representation in debug leads to confusion. A multidimensional array in Java isn't really like that, is just an array of arrays.

Thank you @EthanB21 @Misho Zhghenti

答案3

得分: -1

请发布完整的代码,你究竟写了什么以及调试情况。

重启你的集成开发环境(IDE),并使用以下代码检查一次。

public static void main(String[] args) {
    int[][] grid = new int[9][9];
    int value = 10;
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            grid[i][j] = ++value;
        }
    }

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            System.out.print(((j == 0) ? "" : ", ") + grid[i][j]);
        }
        System.out.println();
    }
}

注意:IDE 也可能出现异常行为,或者可能响应较慢。

英文:

Please post complete code, what exactly you have written and debugging.

Restart your IDE and check once with below code.

public static void main(String[] args) {
    int[][] grid = new int[9][9];
    int value = 10;
    for (int i = 0; i &lt; 9; i++) {
        for (int j = 0; j &lt; 9; j++) {
            grid[i][j] = ++value;
        }
    }

    for (int i = 0; i &lt; 9; i++) {
        for (int j = 0; j &lt; 9; j++) {
            System.out.print(((j == 0) ? &quot;&quot; : &quot;, &quot;) + grid[i][j]);
        }
        System.out.println();
    }
}

Note: IDE may misbehave too or might be responding late.

huangapple
  • 本文由 发表于 2020年9月19日 06:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63963538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定