ArrayListをListに変換できません



Arraylist Cannot Be Converted List



ArrayListを使用する場合、この問題が発生します。

ArrayList cannot be converted to List

説明ジェネリックエラー、ジェネリックタイプが同じであることを確認してください! ! !



例えば:

public class Solution { public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList<TreeNode> nodes = new ArrayList<>() if(root==null){ return null } nodes.add(root) for(int i=0i<nodes.size()i++){ TreeNode node = nodes.get(i) if(node.left!=null){ nodes.add(node.left) } if(node.right!=null){ nodes.add(node.right) } } return nodes } }

ここでノードTreeNodeは汎用であり、最後の呼び出しは整数型であるため、エラーが発生します。