我正在使用给定的接口编写哈希表,但我得到的错误是
中的标题return (IEnumerator<Key>)items.GetEnumerator();
和
foreach (String first in ht);
整个代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RIT_CS
{
public class NonExistentKey<Key> : Exception
{
public Key BadKey { get; private set; }
public NonExistentKey(Key k) :
base("Non existent key in HashTable: " + k)
{
BadKey = k;
}
}
interface Table<Key, Value> : IEnumerable<Key>
{
void Put(Key k, Value v);
bool Contains(Key k);
Value Get(Key k);
}
public struct KeyValue<Key, Value>
{
public Key K { get; set;}
public Value V { get; set; }
}
class MyTable<Key, Value>:Table<Key, Value>
{
private int size;
private int count=0;
private LinkedList<KeyValue<Key, Value>>[] items;
public MyTable(int size)
{
...
}
public void SetSize(int size)
{
...
}
public LinkedList<KeyValue<Key, Value>>[] GetItems()
{
return items;
}
protected int Getposition(Key k)
{
...
}
protected LinkedList<KeyValue<Key, Value>> GetLinkedList(int position)
{
return items[position];
}
public void Put(Key k, Value v)
{
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return (System.Collections.IEnumerator)GetEnumerator();
}
public IEnumerator<Key> GetEnumerator()
{
return (IEnumerator<Key>)items.GetEnumerator();
}
public bool Contains(Key k)
{
}
public Value Get(Key k)
{
}
}
class TableFactory
{
public static MyTable<Key, Value> Make<Key, Value>(int size = 100, double loadThreshold = 0.75)
{
MyTable<Key, Value> mytable = new MyTable<Key, Value>(size);
return mytable;
}
}
class MainClass
{
public static void Main(string[] args)
{
MyTable<String, String> ht = TableFactory.Make<String, String>(10, 0.75);
ht.Put("Joe", "Doe");
ht.Put("Jane", "Brain");
ht.Put("Chris", "Swiss");
try
{
foreach (String first in ht)
{
Console.WriteLine(first + " -> " + ht.Get(first));
}
Console.WriteLine("=========================");
ht.Put("Wavy", "Gravy");
ht.Put("Chris", "Bliss");
foreach (String first in ht)
{
Console.WriteLine(first + " -> " + ht.Get(first));
}
Console.WriteLine("=========================");
Console.Write("Jane -> ");
Console.WriteLine(ht.Get("Jane"));
Console.Write("John -> ");
Console.WriteLine(ht.Get("John"));
}
catch (NonExistentKey<String> nek)
{
Console.WriteLine(nek.Message);
Console.WriteLine(nek.StackTrace);
}
Console.ReadLine();
}
}
}
为了看起来更好,我删除了一些不相关的代码。
答案 0 :(得分:1)
items
字段声明为
private LinkedList<KeyValue<Key, Value>>[] items;
根据MSDN LinkedList<T>
实施IEnumerable<T>
,在您的情况下为IEnumerable<KeyValue<Key, Value>>
。
因此,假设IEnumerable<T>.GetEnumerator()
返回IEnumerator<T>
,在您的情况下又是IEnumerator<KeyValue<Key, Value>>
,则无法将其强制转换为IEnumerator<Key>
。它不起作用。
您可以尝试以下操作:
public IEnumerator<Key> GetEnumerator()
{
return items.Select(x => x.K).GetEnumerator();
}
答案 1 :(得分:0)
LinkedList<KeyValuePair<int, string>> items = new LinkedList<KeyValuePair<int, string>>();
items.AddFirst(new KeyValuePair<int, string>(1, "qqq"));
items.AddFirst(new KeyValuePair<int, string>(2, "www"));
items.AddFirst(new KeyValuePair<int, string>(3, "eee"));
var keys = items.Select(item => item.Key);
foreach (var key in keys)
{
}
答案 2 :(得分:0)
只需使用https://stackoverflow.com/a/1272677/3162415中的第一个选项并写下:
return ((IEnumerable<Key>)items).GetEnumerator();