destello :
Tengo una List<List<Integer>> arr
que es 2D_INTEGER_ARRAY
. Necesito convertir esto en 2D char[][]
matriz. Probé con código de abajo pero está dando cuestión compilación que es obvio, pero no es capaz de averiguar cómo puedo hacer eso?
public static int largestMatrix(List<List<Integer>> arr) {
char[][] matrix = new char[arr.size()][];
for (int i = 0; i < arr.size(); i++) {
List<Integer> row = arr.get(i);
// below line is giving error
matrix[i] = row.toArray(new char[row.size()]);
}
}
El error es:
[Java] The method toArray(T[]) in the type List<Integer> is not applicable for the arguments (char[])
shmosel:
Integer
y char
son tipos independientes. Si desea un número entero representado como un dígito, es necesario convertirla (casting sólo le dará la representación ASCII). Además, no se puede llamar toArray()
con una matriz primitiva. Vas a tener que iterate y convertir de forma manual:
matrix[i] = new char[row.size()];
for (int j = 0; j < row.size(); j++) {
matrix[i][j] = Character.forDigit(row.get(j), 10);
}