代码之家  ›  专栏  ›  技术社区  ›  netmajor

将泛型集合添加到LinkedList问题

  •  0
  • netmajor  · 技术社区  · 15 年前

    我有收藏:

     class HashedData : IComparable
    {
        string key;
    
        public string Key
        {
            get { return key; }
            set { key = value; }
        }
        string value;
    
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    
    
        public int CompareTo(object obj)
        {
            if (obj is HashedData)
            {
                HashedData hd = (HashedData)obj;
                return this.Key.CompareTo(hd.Key);
            }
            throw new NotImplementedException("Obiekt nie jest typu HashData");
        }
    }
    

    以及将此集合用作类型od LinkedList的方法:

            public void AddToHashTable(List<LinkedList<HashedData>> tablicaHashująca, HashedData data)
        {
            tablicaHashujaca[this.Hashuj(data.Key)].AddLast(**data**);
        }
    

    我有错误:

    错误1与“System.Collections.Generic.LinkedList.AddLast(String)”匹配的最佳重载方法具有一些无效参数

    错误2参数1:无法从“hashowanie.hasheddata”转换为“string”

    但是为什么我不能向LinkedList添加hashdata对象呢?有什么办法吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   TalentTuner    15 年前

    我可以这样做,如果您定义LinkedList<HashedData>,那么当您调用AddLast时,它将允许您在LinkedList中添加HashedData的对象。

    我看不出有什么问题,请你多发些密码好吗

        2
  •  0
  •   leppie    15 年前
    data.Value
    

    不确定这是否是你想要的。