本文整理汇总了C#中HashTable.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.Clear方法的具体用法?C# HashTable.Clear怎么用?C# HashTable.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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, int> hash = new HashTable<string, int>(32);
hash.Add("Pesho", 6);
hash.Add("Pesho", 6);
hash.Add("Pesho", 3);
hash.Add("Gosho", 9);
hash.Add("Tosho", 4);
hash.Add("Viko", 7);
hash.Add("Viko", 7);
hash.Add("high 5", 7);
hash["Johnatan"] = 3333;
hash["viko"] = 1111111;
hash.Remove("Tosho");
foreach (var item in hash)
{
Console.WriteLine("{0} -> {1}", item.Key, item.Value);
}
hash.Clear();
foreach (var item in hash)
{
Console.WriteLine("{0} -> {1}", item.Key, item.Value);
}
}
示例3: ClearShouldEmptyTheHashTable
public void ClearShouldEmptyTheHashTable()
{
var testHashTable = new HashTable<int, int>();
testHashTable.Set(1, 1);
testHashTable.Clear();
Assert.AreEqual(0, testHashTable.Count);
}
示例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: ClearShouldEmptyTheTableAndKeepTheCurrentCapacity
public void ClearShouldEmptyTheTableAndKeepTheCurrentCapacity()
{
var table = new HashTable<string, List<int>>();
for (int i = 0; i < 50; i++)
{
table[i.ToString()] = new List<int>();
for (int j = 0; j < 5; j++)
{
table[i.ToString()].Add(j);
}
Assert.IsTrue(table[i.ToString()].Count > 0);
}
int finalCapacity = table.Capacity;
table.Clear();
Assert.IsTrue(finalCapacity >= 50);
Assert.AreEqual(finalCapacity, table.Capacity);
Assert.AreEqual(0, table.Count);
for (int i = 0; i < 50; i++)
{
Assert.IsFalse(table.ContainsKey(i.ToString()));
}
}
示例6: 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);
}
示例7: Main
public static void Main()
{
var topCities = new HashTable<string, double>();
// Add(key, value)
topCities.Add("Tokyo", 36.9);
topCities.Add("Jakarta", 12.8);
topCities.Add("Seoul", 25.7);
topCities.Add("Beijing", 23.7);
topCities.Add("Shanghai", 21.7);
// foreach
foreach (var pair in topCities.OrderBy(p => -p.Value))
{
Console.WriteLine("{0, -8} -> {1}", pair.Key, pair.Value);
}
// Add already existing key
try
{
topCities.Add("Jakarta", 0);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
// Keys
Console.WriteLine("Keys: {0}", string.Join(", ", topCities.Keys));
// Count
Console.WriteLine("Number of key-value pairs: {0}", topCities.Count);
// Find(key)
topCities.PrintValue("Tokyo");
Console.WriteLine("Jakarta -> {0}", topCities.Find("Jakarta"));
// this[key]
Console.WriteLine("Beijing -> {0}", topCities["Beijing"]);
// ContainsKey(key)
Console.WriteLine("Hash table contains 'Delhi': {0}", topCities.ContainsKey("Delhi"));
// Remove(key)
topCities.Remove("Seoul");
Console.WriteLine("Remove 'Seoul'");
Console.WriteLine("Keys: {0}", string.Join(", ", topCities.Keys));
Console.WriteLine("Number of key-value pairs: {0}", topCities.Count);
// Remove non-existing
Console.WriteLine("Removing 'Sofia' -> {0}", topCities.Remove("Sofia"));
// Clear
topCities.Clear();
Console.WriteLine("Clear hash table");
Console.WriteLine("Number of key-value pairs: {0}", topCities.Count);
}
示例8: ClearNewKeyPairShouldWorkCorrectly
public void ClearNewKeyPairShouldWorkCorrectly()
{
var hashTable = new HashTable<string, int>();
hashTable.Add("Ivan", 1);
hashTable.Add("Ivan1", 2);
hashTable.Add("Ivan2", 3);
hashTable.Clear();
Assert.AreEqual(hashTable.Count, 0);
}
示例9: ClearShouldClearCount
public void ClearShouldClearCount()
{
var hashtable = new HashTable<int, int>();
hashtable.Add(10, 15);
Assert.AreEqual(1, hashtable.Count);
hashtable.Clear();
Assert.AreEqual(0, hashtable.Count);
}
示例10: Main
static void Main(string[] args)
{
HashSet<int> p = new HashSet<int>();
HashTable<int, int> hashTable = new HashTable<int, int>(2);
hashTable.Add(3, 4);
hashTable.Add(7, 4);
Console.WriteLine(hashTable.Count);
hashTable.Remove(3);
Console.WriteLine(hashTable.Count);
hashTable.Add(3, 4);
hashTable.Add(8, 4);
hashTable.Add(13, 4);
hashTable.Add(3333, 4);
hashTable.Add(124, 4);
hashTable.Add(412412, 4);
hashTable[8] = 25;
hashTable[8] = -45;
Console.WriteLine(hashTable.Count);
foreach (var item in hashTable)
{
Console.WriteLine("Key {0}, Value {1}", item.Key, item.Value);
}
// Console.WriteLine(hashTable.Count);
// Console.WriteLine(hashTable.Find(3));
IEnumerable<int> keys = hashTable.Keys;
foreach (var key in keys)
{
Console.Write(key + ",");
}
Console.WriteLine();
hashTable.Add(17, 34);
hashTable.Remove(8);
hashTable.Remove(3333);
hashTable.Add(-4, 23);
keys = hashTable.Keys;
foreach (var key in keys)
{
Console.Write(key + ",");
}
Console.WriteLine();
hashTable.Clear();
Console.WriteLine(hashTable.Count);
foreach (var item in hashTable)
{
Console.WriteLine("Key {0}, Value {1}", item.Key, item.Value);
}
}
示例11: ClearShouldResultInKeysCountOfZeroAndCountOfZero
public void ClearShouldResultInKeysCountOfZeroAndCountOfZero()
{
var hashTable = new HashTable<int, string>();
int key = 5;
string value = "master";
hashTable.Add(key, value);
hashTable.Clear();
bool expected = true;
Assert.AreEqual(expected, hashTable.Keys.Count() == 0);
Assert.AreEqual(expected, hashTable.Count == 0);
}
示例12: ShouldClearAllElements
public void ShouldClearAllElements()
{
var hashtable = new HashTable<int, string>();
int numberOfAddedElements = 5;
for (int i = 0; i < numberOfAddedElements; i++)
{
hashtable.Add(i, "test");
Assert.AreEqual(i + 1, hashtable.Count);
}
hashtable.Clear();
Assert.AreEqual(0, hashtable.Keys.Count);
}
示例13: TestHashTableClear
public void TestHashTableClear()
{
HashTable<int, int> integerTable = new HashTable<int, int>();
int numberOfElements = 10;
for (int i = 0; i < numberOfElements; i++)
{
integerTable.Add(i, i);
}
integerTable.Clear();
int actualCount = integerTable.Count;
Assert.AreEqual(0, actualCount, "The table count is incorrect when table is cleared!");
}
示例14: TestIfClearLeavesAnEmptyTable
public void TestIfClearLeavesAnEmptyTable()
{
var table = new HashTable<int, int>();
table.Add(1, 11);
table.Add(2, 12);
table.Add(3, 13);
table.Add(4, 14);
table.Add(5, 15);
table.Add(6, 16);
table.Add(7, 17);
table.Add(8, 18);
table.Clear();
Assert.AreEqual(0, table.Count);
}
示例15: Main
static void Main()
{
HashTable<string, int> myPeopleAge = new HashTable<string, int>();
Console.WriteLine("MY PEOPLE");
myPeopleAge.Add("Siabonga", 20);
myPeopleAge.Add("Huren", 21);
myPeopleAge["Filip"] = 22;
myPeopleAge["Garo"] = 18;
Console.WriteLine("count="+ myPeopleAge.Count());
foreach (var item in myPeopleAge)
{
Console.WriteLine(item);
}
Console.WriteLine();
Console.WriteLine("MY PEOPLE AFTER REMOVE FILIP AND GARO");
myPeopleAge.Remove("Filip");
myPeopleAge.Remove("Garo");
Console.WriteLine("count="+myPeopleAge.Count());
foreach (var item in myPeopleAge)
{
Console.WriteLine(item);
}
Console.WriteLine();
Console.WriteLine("ONLY THE KEYS");
List<string> myKeys = myPeopleAge.Keys;
for (int i = 0; i < myKeys.Count; i++)
{
Console.WriteLine(myKeys[i]);
}
Console.WriteLine();
Console.Write("Find Siabonga age: ");
Console.Write(myPeopleAge.Find("Siabonga"));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("AFTER CLEAR COMAND");
myPeopleAge.Clear();
Console.WriteLine("count="+myPeopleAge.Count());
Console.WriteLine();
//Console.Write("Find Oncho(I do not have Oncho) age: ");
//Console.Write(myPeopleAge.Find("Oncho"));
}