本文整理汇总了C#中HashTable.Keys方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.Keys方法的具体用法?C# HashTable.Keys怎么用?C# HashTable.Keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable.Keys方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
/* Task 4:
* Implement the data structure "hash table" in a class HashTable<K,T>.
* Keep the data in array of lists of key-value pairs (LinkedList<KeyValuePair<K,T>>[])
* with initial capacity of 16. When the hash table load runs over 75%, perform resizing to 2
* times larger capacity. Implement the following methods and properties: Add(key, value), Find(key)value,
* Remove( key), Count, Clear(), this[], Keys. Try to make the hash table to support iterating over its elements
* with foreach.
*/
//Few functionalities are missing. If I have time I will add them as well.
static void Main(string[] args)
{
var hashTable = new HashTable<string, int>();
for (int i = 0; i < 100; i++)
{
hashTable.Add("Entri" + i, i);
}
Console.WriteLine(hashTable.Count());
Console.WriteLine(hashTable.Find("Entri50"));
for (int i = 50; i < 100; i++)
{
hashTable.Remove("Entri" + i);
}
//Will throw an exception
//Console.WriteLine(hashTable.Find("Entri50"));
var keys = hashTable.Keys();
foreach (var key in keys)
{
Console.WriteLine(key);
}
hashTable.Clear();
Console.WriteLine(hashTable.Count());
}
示例2: Main
static void Main()
{
HashTable<string, double> table = new HashTable<string, double>();
table.Add("a", 1.0);
table.Add("aaa", 1.4);
table.Add("z", 1.7);
foreach (var item in table)
{
Console.WriteLine("{0} key {1} value", item.Key, item.Value);
}
table.Remove("a");
Console.WriteLine(table.Find("aaa"));
Console.WriteLine(table["z"]);
Console.WriteLine(table.Count);
List<string> keys = table.Keys();
table.Clear();
}
示例3: Main
internal static void Main()
{
var myLetters = new HashTable<string, int>();
myLetters.Add("a", 1);
myLetters.Add("b", 1);
myLetters.Add("c", 1);
myLetters.Add("d", 1);
myLetters.Add("e", 1);
myLetters.Add("f", 1);
myLetters.Add("g", 1);
myLetters.Add("h", 1);
myLetters.Add("i", 1);
myLetters.Add("j", 1);
myLetters.Add("k", 1);
myLetters.Add("l", 1);
myLetters.Add("m", 1);
myLetters.Add("n", 1);
myLetters.Add("o", 1);
myLetters.Add("p", 1);
myLetters.Add("q", 1);
myLetters.Add("r", 1);
myLetters.Add("s", 1);
Console.WriteLine(string.Join(", ", myLetters));
Console.WriteLine();
myLetters["o"] = 6;
Console.WriteLine("Now we change letter \"{0}\" to {1} times.", "o", myLetters["o"]);
Console.WriteLine(string.Join(", ", myLetters));
Console.WriteLine();
myLetters.Remove("q");
Console.WriteLine("After removal of letter \"q\": {0}", string.Join(", ", myLetters));
Console.WriteLine();
Console.WriteLine("MyLetters contain \"z\"? {0}", myLetters.ContainsKey("z"));
Console.WriteLine("MyLetters contain \"a\"? {0}", myLetters.ContainsKey("a"));
Console.WriteLine();
Console.WriteLine("Keys: {0}", string.Join(", ", myLetters.Keys()));
Console.WriteLine();
myLetters.Clear();
Console.WriteLine("MyLetters after clear: {0}", string.Join(", ", myLetters));
}
示例4: Main
static void Main(string[] args)
{
HashTable<string, int> sample = new HashTable<string, int>();
sample.Add("1151", 1);
sample.Add("1", 23);
sample.Add("1", 2);
sample.Add("19", 4);
sample.Add("3", 3);
sample.Add("19", 3);
sample.Add("3", 15);
sample.Add("2", 15);
sample.Add("7", 15);
sample.Add("9", 15);
sample.Add("-11", 15);
sample.Add("-33", 15);
sample.Add("9", 15);
sample.Add("0", 15);
sample.Add("101", 15);
sample.Add("102", 15);
sample.Add("103", 15);
sample.Add("104", 15);
sample.Add("105", 15);
foreach (LinkedList<KeyValuePair<string,int>> item in sample)
{
Console.WriteLine("Key:" + item.First.Value.Key);
foreach (var element in item)
{
Console.WriteLine(" Values:" + element.Value);
}
}
Console.WriteLine("________________________________________");
Console.WriteLine("Values for Key of 3:");
int[] founded = sample.Find("3");
for (int i = 0; i < founded.Length; i++)
{
Console.WriteLine(" Values:" +founded[i]);
}
Console.WriteLine("________________________________________");
Console.WriteLine("Remove Key of 19:");
sample.Remove("19");
foreach (LinkedList<KeyValuePair<string, int>> item in sample)
{
Console.WriteLine("Key:" + item.First.Value.Key);
foreach (var element in item)
{
Console.WriteLine(" Values:" + element.Value);
}
}
Console.WriteLine("________________________________________");
Console.WriteLine("List of keys:");
string[] keys = sample.Keys();
foreach (var item in keys)
{
Console.WriteLine(item);
}
sample.Clear();
Console.WriteLine("________________________________________");
Console.WriteLine("After clear Count = " + sample.Count);
}
示例5: Main
public static void Main(string[] args)
{
LinkedList<KeyValuePair<string, int>> list = new LinkedList<KeyValuePair<string, int>>();
Person person1 = new Person("Pesho", "Peshov", 21);
Person person2 = new Person("Gosho", "Peshov", 22);
Person person3 = new Person("Tosho", "Peshov", 23);
// Console.WriteLine(person1.GetHashCode());
// Console.WriteLine(person2.GetHashCode());
// Console.WriteLine(person3.GetHashCode());
// Console.WriteLine(person1.CompareTo(person2));
// Console.WriteLine(person1.CompareTo(person3));
var hashTable = new HashTable<Person, int>();
hashTable.Add(person1, 1);
hashTable.Add(person2, 1);
hashTable.Add(person3, 2);
foreach (KeyValuePair<Person, int> person in hashTable)
{
Console.WriteLine("Key\n{0}Value: {1}", person.Key, person.Value);
Console.WriteLine();
}
Console.WriteLine("=====FIND=====");
Console.WriteLine(hashTable.Find(person1));
Console.WriteLine("=====REMOVE=====");
Console.WriteLine(hashTable.Remove(person1));
Console.WriteLine(hashTable.Remove(person1));
Console.WriteLine("=====FIND=====");
Console.WriteLine(hashTable.Find(person1));
var keys = hashTable.Keys();
foreach (var item in keys)
{
Console.WriteLine(item);
}
Console.WriteLine("=====INDEXER=====");
Person person4 = new Person("Rosho", "Peshov", 55);
// set
hashTable[person4] = 777;
// get
Console.WriteLine(hashTable[person4]);
Console.WriteLine("=====CLEAR=====");
hashTable.Clear();
Person person5 = new Person("Josh", "Goshov", 33);
hashTable.Add(person5, 1234);
Person person6 = new Person("Losh", "Goshov", 66);
hashTable.Add(person6, 126);
foreach (KeyValuePair<Person, int> person in hashTable)
{
Console.WriteLine("Key\n{0}Value: {1}", person.Key, person.Value);
Console.WriteLine();
}
Console.WriteLine(hashTable.ContainesKey(person6));
Console.WriteLine("=================================================");
Console.WriteLine("=====HashSet=====");
Hashset<Person> hashSet = new Hashset<Person>();
hashSet.Add(person1);
hashSet.Add(person2);
Console.WriteLine(hashSet.Find(person1));
Console.WriteLine(hashSet.Remove(person1));
Console.WriteLine(hashSet.Find(person1));
Console.WriteLine(hashSet.Find(person2));
Console.WriteLine(hashSet.Remove(person2));
Console.WriteLine("=====FOREACH=====");
hashSet.Clear();
hashSet.Add(person1);
hashSet.Add(person2);
foreach (var item in hashSet)
{
Console.WriteLine(item);
}
Console.WriteLine("=====UNION=====");
var secondHashSet = new Hashset<Person>();
secondHashSet.Add(person2);
secondHashSet.Add(person3);
var unionHashset = hashSet.Union(secondHashSet);
foreach (var item in unionHashset)
{
Console.WriteLine(item);
}
Console.WriteLine("=====INTERSECT ====");
var intersectionHashset = hashSet.Intersect(secondHashSet);
foreach (var item in intersectionHashset)
{
Console.WriteLine(item);
}
}
示例6: TestHashTableGetKeys
public void TestHashTableGetKeys()
{
HashTable<int, int> integerTable = new HashTable<int, int>();
int numberOfElements = 10;
for (int i = 0; i < numberOfElements; i++)
{
integerTable.Add(i, i + 10);
}
var keys = integerTable.Keys();
for (int i = 0; i < keys.Length; i++)
{
Assert.AreEqual(i, keys[i], "Returning incorrect keys!");
}
}