convert List<List<Integer>> to 2D char array?

flash :

I have a List<List<Integer>> arr which is 2D_INTEGER_ARRAY. I need to convert this to 2D char[][] array. I tried with below code but it is giving compilation issue which is obvious but not able to figure out how can I do that?

   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()]);
    }
   }

Error is:

[Java] The method toArray(T[]) in the type List<Integer> is not applicable for the arguments (char[])
shmosel :

Integer and char are separate types. If you want an integer represented as a digit, you need to convert it (casting will only give you the ASCII representation). Besides, you can't call toArray() with a primitive array. You'll have to iterate and convert manually:

matrix[i] = new char[row.size()];
for (int j = 0; j < row.size(); j++) {
    matrix[i][j] = Character.forDigit(row.get(j), 10);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=114856&siteId=1