简体   繁体   English

无法转换列表<List<int> &gt; 返回类型 IList <IList<int> &gt;

[英]Unable to convert List<List<int>> to return type IList<IList<int>>

For level order traversal why does this exception occur?对于层序遍历为什么会出现这个异常? Following exception occurs:出现以下异常:

Cannot implicitly convert type ' System.Collections.Generic.List<System.Collections.Generic.List<int>> ' to ' System.Collections.Generic.IList<System.Collections.Generic.IList<int>> '.无法将类型“ System.Collections.Generic.List<System.Collections.Generic.List<int>> ”隐式转换为“ System.Collections.Generic.IList<System.Collections.Generic.IList<int>> ”。 An explicit conversion exists (are you missing a cast?)存在显式转换(您是否缺少演员表?)

public IList<IList<int>> LevelOrder(TreeNode root) 
{
    var result = new List<List<int>>();
    var que = new Queue<TreeNode>();

    //if(root==null) return result;

    que.Enqueue(root);
    while(que.Count!=0)
    {
        int n = que.Count;
        var subList = new List<int>();
        for(int i=0;i<n;i++)
        {
            if(que.Peek().left!=null) 
                que.Enqueue(que.Peek().left);
            if(que.Peek().right!=null)
                que.Enqueue(que.Peek().right);
            subList.Add(que.Dequeue().val);
        }
        result.Add(subList);
    }
    return  result;
}

Just change the declaration of your result to List<IList<int>> .只需将结果声明更改为List<IList<int>>

List<T> implements IList<T> , but List<List<T>> does not implement IList<IList<int>> . List<T>实现了IList<T> ,但List<List<T>>没有实现IList<IList<int>> Generic parameters are not covariant or contravariant unless defined that way and IList<T> is not, so the type must match exactly.泛型参数不是协变或逆变的,除非以这种方式定义而IList<T>不是,因此类型必须完全匹配。

public IList<IList<int>> LevelOrder(TreeNode root)
{
    var result = new List<IList<int>>();
    var que = new Queue<TreeNode>();

    //if(root==null) return result;

    que.Enqueue(root);
    while (que.Count != 0)
    {
        int n = que.Count;
        var subList = new List<int>();
        for (int i = 0; i < n; i++)
        {
            if (que.Peek().left != null)
                que.Enqueue(que.Peek().left);
            if (que.Peek().right != null)
                que.Enqueue(que.Peek().right);
            subList.Add(que.Dequeue().val);
        }
        result.Add(subList);
    }
    return result;
}

Pretty sure that if it compiles, doing a cast is a real bad idea.可以肯定的是,如果它可以编译,那么进行强制转换是一个非常糟糕的主意。 Here's why:原因如下:

public class myStupidList : IList<int>
{
//implementation unimportant
}

private void Button_Click(object sender, RoutedEventArgs e)
{
  var result = new List<List<int>>();
  IList<IList<int>> imNotAListofLists = (IList<IList<int>>)result;
  imNotAListofLists.Add(new myStupidList());
  //result is not a very valid variable right now, is it?
}

As mentioned in my comment, these types of collection issues boil down to covariance and contravariance , and .NET does provide a lot of tools for dealing with them.正如我在评论中提到的,这些类型的集合问题归结为covariancecontravariance ,.NET 确实提供了很多工具来处理它们。 (Such as various read-only collections and interfaces) (比如各种只读集合和接口)

..Which, explains why you get your error as well. ..Which 解释了为什么你也会得到你的错误。 There is no implicit cast from List<List> to List<IList> because that cast cannot succeed without breaking type-safety.没有从List<List>List<IList>隐式转换,因为如果不破坏类型安全,转换就不会成功。 (and as @Grax mentioned, neither derives from the other) (正如@Grax 提到的,两者都不是从另一个派生的)

There should be explicit conversion and as shown below:-应该有显式转换,如下所示:-

List<IList<int>> result = new List<IList<int>>();

or要么

var result = new List<IList<int>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM