首页 > 解决方案 > JAVA (Generics, Iterable Interface, Initialize 2-dim Array) 类型不匹配:无法从 int 转换为 Matrix

问题描述

    @SuppressWarnings("unchecked")
    Matrix<Integer>[][] m = (Matrix<Integer>[][]) new Matrix[][] {
        {2,34,532,12},
        {923,6,0,67},
        {32,324,3,13}
    };

有人能告诉我如何以不同的方式初始化 3x4 矩阵吗?我在所有三行中都不断收到“类型不匹配:无法从 int 转换为 Matrix”错误。

标签: arraysgenericsmatrixiterable

解决方案


最好知道您正在使用什么语言以及您尝试使用什么 Matrix 类,但看起来您做的事情有点错误。

# create an int array
int[] i = new int[]{1, 2, 3}

# Create a 2d int array
int[][] i = new int[][]{
  {1, 2},
  {3, 4}
}

# what you have is creating a 2d array, of Matrix<Integer>
Matrix<Integer>[][]

# So either just use a normal int 2d array, or i believe the syntax in C++ is:
Matrix<Integer, 3, 4>

推荐阅读