java - 在 Java 中初始化二维数组

标签 java arrays

  1. 在数组中,我们可以初始化如下内容:

    int[] arr = {1, 2, 3, 4, 5};

  2. 但是对于二维数组,为什么下面不正确?

    public class Main {
        public static void main(String[] args) {
           int[][] arr = new int[3][];
           arr[0] = new int[]{1,2,3}; //This is the correct way
    
           arr[0] = {1,2,3};  // ->Why this is not right? 
        }
    }
    

最佳答案

Yes, you are correct the second way of creating a 2D array will not work because when the compiler will compile the code because it couldn't decide its type.

关于java - 在 Java 中初始化二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51122798/

相关文章:

java - 在 Eclipse 中链接文本资源

java - 存储在 ModelMap 中的 HTML 标签不会被浏览器解释为 HTML

Java读取文本文件2列并存储在数组中

javascript - 如何将变量值与Javascript中的值数组匹配?

java - 如何记录文件中存储的运行sql语句的执行时间?

java - 暂停和恢复 ScheduledExecutorService

java - 如何从一个方法中中断另一个方法?

python - 使用另一个数组的索引索引一个 numpy 数组

c - 你如何在 C 中创建一个指针数组?

php - 搜索多维数组