当前位置: 首页>>代码示例>>C#>>正文


C# HashTable.Keys方法代码示例

本文整理汇总了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());
    }
开发者ID:aleks-todorov,项目名称:HomeWorks,代码行数:41,代码来源:ImplementingHashTable.cs

示例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();
    }
开发者ID:vasilkrvasilev,项目名称:DataStructuresAlgorithms,代码行数:18,代码来源:Example.cs

示例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));
 }
开发者ID:kalinalazarova1,项目名称:TelerikAcademy,代码行数:39,代码来源:HashTableTest.cs

示例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);
        }
开发者ID:vaster,项目名称:Telerik.vasko,代码行数:68,代码来源:Program.cs

示例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);
            }
        }
开发者ID:bstaykov,项目名称:Telerik-DSA,代码行数:98,代码来源:Program.cs

示例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!");
            }
        }
开发者ID:ilkodzhambazov,项目名称:Telerik-Academy,代码行数:16,代码来源:HashTableUnitTests.cs


注:本文中的HashTable.Keys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。