You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实例化存储整数列表的二维List时类型不兼容报错求助

矩阵最长递增路径代码错误分析与修正

核心错误原因

你定义的dp是List<ArrayList<Integer>>[][],意味着每个dp[i][j]是一个存储ArrayList<Integer>的列表,但你尝试往里面添加的是int[]数组,两种类型完全不兼容,这就是报错incompatible types: int[] cannot be converted to ArrayList的直接原因。

你的需求是每个条目存储包含长度为2的整数列表的ArrayList,正确的类型应该是List<List<Integer>>[][](每个元素是存储坐标的List<Integer>),或者用更清晰的自定义类存储坐标。

分步修正方案

1. 调整dp的类型定义

把List<ArrayList<Integer>>[][] dp = new List[matrix.length][matrix[0].length];改为:

List<List<Integer>>[][] dp = new List[matrix.length][matrix[0].length];

这样每个dp[i][j]就可以存储多个长度为2的List<Integer>坐标。

2. 替换int[]为ArrayList

创建坐标时不再用数组,改用ArrayList<Integer>存储坐标值:
原错误代码:

int[] pair = new int[2];
pair[0] = i;
pair[1] = j - 1;
dp[i][j].add(pair);

修正为:

ArrayList<Integer> pair = new ArrayList<>();
pair.add(i);
pair.add(j - 1);
dp[i][j].add(pair);

3. 修正DFS方法的类型错误

原DFS方法的参数ArrayList<Integer> neighbors类型不匹配,需要改为List<List<Integer>> neighbors;同时循环中获取的neighbor要声明为List<Integer>,并修正访问标记的逻辑。

4. 修正最长路径初始值

每个单元格自身的最长路径至少为1,所以longest[i][j]的初始值要设为1,而不是0。

修正后的完整代码

import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;

class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        
        List<List<Integer>>[][] dp = new List[matrix.length][matrix[0].length];
        for (int i = matrix.length - 1; i >= 0; i--) {
            for (int j = matrix[0].length - 1; j >= 0; j--) {
                dp[i][j] = new ArrayList<>();
                // 左邻居
                if (j - 1 >= 0 && matrix[i][j-1] < matrix[i][j]) {
                    ArrayList<Integer> pair = new ArrayList<>();
                    pair.add(i);
                    pair.add(j - 1);
                    dp[i][j].add(pair);
                }
                // 右邻居
                if (j + 1 < matrix[0].length && matrix[i][j+1] < matrix[i][j]) {
                    ArrayList<Integer> pair = new ArrayList<>();
                    pair.add(i);
                    pair.add(j + 1);
                    dp[i][j].add(pair);
                }
                // 上邻居
                if (i - 1 >= 0 && matrix[i-1][j] < matrix[i][j]) {
                    ArrayList<Integer> pair = new ArrayList<>();
                    pair.add(i - 1);
                    pair.add(j);
                    dp[i][j].add(pair);
                }
                // 下邻居
                if (i + 1 < matrix.length && matrix[i+1][j] < matrix[i][j]) {
                    ArrayList<Integer> pair = new ArrayList<>();
                    pair.add(i + 1);
                    pair.add(j);
                    dp[i][j].add(pair);
                }
            }
        }

        int[][] longest = new int[matrix.length][matrix[0].length];
        HashSet<List<Integer>> visited = new HashSet<>();
        
        for (int i = matrix.length - 1; i >= 0; i--) {
            for (int j = matrix[0].length - 1; j >= 0; j--) {
                longest[i][j] = 1; // 每个单元格自身长度为1
                dfs(visited, dp[i][j], i, j, longest, dp, matrix);
            }
        }

        int maxLength = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (longest[i][j] > maxLength) {
                    maxLength = longest[i][j];
                }
            }
        }

        return maxLength;
    }

    public void dfs(HashSet<List<Integer>> visited, List<List<Integer>> neighbors, int x, int y, int[][] longest, List<List<Integer>>[][] dp, int[][] matrix) {
        for (List<Integer> neighbor : neighbors) {
            int n_x = neighbor.get(0);
            int n_y = neighbor.get(1);
            List<List<Integer>> neighborList = dp[n_x][n_y];
            
            if (!visited.contains(neighbor)) {
                visited.add(neighbor);
                dfs(visited, neighborList, n_x, n_y, longest, dp, matrix);
                visited.remove(neighbor); // 回溯移除标记
            }
            
            if (longest[n_x][n_y] + 1 > longest[x][y]) {
                longest[x][y] = longest[n_x][n_y] + 1;
            }
        }
    }
}

额外优化建议

  • 使用Java 16+的记录类存储坐标,代码可读性会大幅提升:
    record Coordinate(int x, int y) {}
    
    此时dp的类型可以改为List<Coordinate>[][],避免用List<Integer>存储坐标的歧义。
  • 可以直接用记忆化DFS替代预处理邻居列表的逻辑,减少空间复杂度,同时简化代码。

内容的提问来源于stack exchange,提问作者Vikram Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 23:20:57