我在实例化泛型类型数组时遇到问题,这是我的代码:
public final class MatrixOperations<T extends Number>
{
/**
* <p>This method gets the transpose of any matrix passed in to it as argument</p>
* @param matrix This is the matrix to be transposed
* @param rows The number of rows in this matrix
* @param cols The number of columns in this matrix
* @return The transpose of the matrix
*/
public T[][] getTranspose(T[][] matrix, int rows, int cols)
{
T[][] transpose = new T[rows][cols];//Error: generic array creation
for(int x = 0; x < cols; x++)
{
for(int y = 0; y < rows; y++)
{
transpose[x][y] = matrix[y][x];
}
}
return transpose;
}
}
我只希望这个方法能够转置一个矩阵,它的类是 Number 的子类型,并返回指定类型的矩阵的转置。任何人的帮助将不胜感激。谢谢。