本文整理汇总了C#中HashTable.Add方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.Add方法的具体用法?C# HashTable.Add怎么用?C# HashTable.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
var hashtable = new HashTable<int, string>();
hashtable.Add(5, "five");
hashtable.Add(3, "tree");
hashtable.Add(-6, "minus six");
Console.WriteLine(hashtable.Find(-6));
Console.WriteLine("All elements");
foreach (var item in hashtable)
{
Console.WriteLine("Key: {0} => Value: {1}", item.Key, item.Value);
}
hashtable.Remove(3);
Console.WriteLine("3 removed from table");
try
{
Console.WriteLine("Searching for 3 in table");
hashtable.Find(3);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Hash table has {0} elements", hashtable.Count);
}
示例2: InsertValueByDuplicatedKey_ExpectedException
public void InsertValueByDuplicatedKey_ExpectedException()
{
HashTable<int, int> hashTable = new HashTable<int, int>(10);
hashTable.Add(1, 1);
hashTable.Add(1, 2);
}
示例3: AddDuplicateKeyElementsTest
public void AddDuplicateKeyElementsTest()
{
HashTable<int, char> alphabet = new HashTable<int, char>();
alphabet.Add(1, 'A');
alphabet.Add(1, 'a');
}
示例4: Main
static void Main(string[] args)
{
HashTable<int, string> table = new HashTable<int, string>();
for (int i = 0; i < 17; i++)
{
table.Add(i, (i + 1).ToString());
}
Console.WriteLine(table[15]);
table.Clear();
for (int i = 0; i < 3000; i++)
{
table.Add(i, (i + 1).ToString());
}
Console.WriteLine(table[1457]);
//table.Add(1, "test");
//table.Add(2, "test");
////table.Remove(1);
//Console.WriteLine(table[1]);
}
示例5: AddShouldThrowExceptionWhenTheSameKeyIsAlreadyPresent
public void AddShouldThrowExceptionWhenTheSameKeyIsAlreadyPresent()
{
var hashtable = new HashTable<string, int>();
hashtable.Add("gosho", 12);
hashtable.Add("gosho", 2);
}
示例6: Main
static void Main()
{
var grades = new HashTable<string, int>();
Console.WriteLine("Grades:" + string.Join(",", grades));
Console.WriteLine("--------------------");
grades.Add("Peter", 3);
grades.Add("Maria", 6);
grades["George"] = 5;
Console.WriteLine("Grades:" + string.Join(",", grades));
Console.WriteLine("--------------------");
grades.AddOrReplace("Peter", 33);
grades.AddOrReplace("Tanya", 4);
grades["George"] = 55;
Console.WriteLine("Grades:" + string.Join(",", grades));
Console.WriteLine("--------------------");
Console.WriteLine("Keys: " + string.Join(", ", grades.Keys));
Console.WriteLine("Values: " + string.Join(", ", grades.Values));
Console.WriteLine("Count = " + string.Join(", ", grades.Count));
Console.WriteLine("--------------------");
grades.Remove("Peter");
grades.Remove("George");
grades.Remove("George");
Console.WriteLine("Grades:" + string.Join(",", grades));
Console.WriteLine("--------------------");
Console.WriteLine("ContainsKey[\"Tanya\"] = " + grades.ContainsKey("Tanya"));
Console.WriteLine("ContainsKey[\"George\"] = " + grades.ContainsKey("George"));
Console.WriteLine("Grades[\"Tanya\"] = " + grades["Tanya"]);
Console.WriteLine("--------------------");
}
示例7: Main
internal static void Main()
{
string decorationLine = new string('-', Console.WindowWidth);
Console.Write(decorationLine);
Console.WriteLine("***Presenting the functionality of the data structure 'Hash table'***");
Console.Write(decorationLine);
HashTable<string, int> clubsPoints = new HashTable<string, int>();
Console.WriteLine("---Add operation---");
clubsPoints.Add("Rabbits-O!", 15);
clubsPoints.Add("Frogssy", 19);
clubsPoints.Add("Puma Runners", 17);
clubsPoints.Add("Lion Kings", 33);
Console.WriteLine("Count = " + clubsPoints.Count);
Console.WriteLine();
Console.WriteLine("---Iterator functionality---");
PrintClubsPoints(clubsPoints);
Console.WriteLine();
Console.WriteLine("---Remove operation---");
bool isRemoved = clubsPoints.Remove("Frogssy");
Console.WriteLine("Are 'Frogssy' removed: " + isRemoved);
Console.WriteLine("Count = " + clubsPoints.Count);
Console.WriteLine("Hash table after removal:");
PrintClubsPoints(clubsPoints);
Console.WriteLine();
Console.WriteLine("---Indexer---");
Console.WriteLine("'Lion Kings' points so far: " + clubsPoints["Lion Kings"]);
clubsPoints["Rabbits-O!"] += 3;
Console.WriteLine("'Rabbits-O!' points after a win: " + clubsPoints["Rabbits-O!"]);
Console.WriteLine();
Console.WriteLine("---Find operation---");
Console.WriteLine("Info about 'Puma Runners'" + clubsPoints.Find("Puma Runners"));
Console.WriteLine();
Console.WriteLine("---ContainsKey operation---");
Console.WriteLine("Is there a club 'Birdy'? - " + clubsPoints.ContainsKey("Birdy"));
Console.WriteLine("Is there a club 'Rabbits-O!'? - " + clubsPoints.ContainsKey("Rabbits-O!"));
Console.WriteLine();
Console.WriteLine("---Keys property---");
Console.Write("All clubs names are: ");
Console.WriteLine(string.Join(", ", clubsPoints.Keys));
Console.WriteLine();
Console.WriteLine("---Values property---");
Console.Write("All club's points are: ");
Console.WriteLine(string.Join(", ", clubsPoints.Values));
Console.WriteLine();
Console.WriteLine("---Clear operation---");
clubsPoints.Clear();
Console.WriteLine("Elements count after clearing: " + clubsPoints.Count);
}
示例8: SetNonExistingElementTest
public void SetNonExistingElementTest()
{
HashTable<int, char> alphabet = new HashTable<int, char>();
alphabet.Add(1, 'A');
alphabet.Add(2, 'B');
alphabet.Add(3, 'C');
alphabet[4] = 'D';
}
示例9: GetNonExistingElementTest
public void GetNonExistingElementTest()
{
HashTable<int, char> alphabet = new HashTable<int, char>();
alphabet.Add(1, 'A');
alphabet.Add(2, 'B');
alphabet.Add(3, 'C');
char dLetter = alphabet[4];
}
示例10: AddingNewKeyPairShouldCountCorrectly
public void AddingNewKeyPairShouldCountCorrectly()
{
var hashTable = new HashTable<string, int>();
hashTable.Add("Ivan", 1);
hashTable.Add("Ivan1", 2);
hashTable.Add("Ivan2", 3);
Assert.AreEqual(hashTable.Count, 3);
}
示例11: TestIfAddingASameKeyThrows
public void TestIfAddingASameKeyThrows()
{
var table = new HashTable<int, int>();
table.Add(1, 11);
table.Add(1, 12);
var count = table.Count;
Assert.AreEqual(8, count);
}
示例12: HashTableTest
public void HashTableTest()
{
HashTable<string, int> table = new HashTable<string, int>(100);
table.Add("brad", 100);
table.Add("rich", 200);
table.Remove("rich");
Assert.AreEqual(100, table.Find("brad"));
Assert.AreEqual(0, table.Find("rich"));
}
示例13: Add_EmptyHashTable_Duplicates_ShouldThrowException
public void Add_EmptyHashTable_Duplicates_ShouldThrowException()
{
// Arrange
var hashTable = new HashTable<string, string>();
// Act
hashTable.Add("Peter", "first");
hashTable.Add("Peter", "second");
}
示例14: FindNonExistingEntryTest
public void FindNonExistingEntryTest()
{
HashTable<string, int> studentMarks = new HashTable<string, int>();
studentMarks.Add("Ivan", 5);
studentMarks.Add("Pesho", 4);
studentMarks.Add("Lili", 6);
studentMarks.Add("Gosho", 3);
studentMarks.Find("Marin");
}
示例15: Add_GivenKeyAndValue_PairInATable
public void Add_GivenKeyAndValue_PairInATable()
{
var hashTable = new HashTable<int, int>();
hashTable.Add(5, 2);
hashTable.Add(48, 30);
hashTable.Add(11, 7);
hashTable.Add(78, 25);
hashTable.Add(7, 58);
Assert.AreEqual(7, hashTable.SearchByKey(58));
}