本文整理汇总了C#中HashedSet.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# HashedSet.Clear方法的具体用法?C# HashedSet.Clear怎么用?C# HashedSet.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashedSet
的用法示例。
在下文中一共展示了HashedSet.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
var set = new HashedSet<int>();
set.Add(5);
set.Add(3);
set.Add(-4);
set.Add(12);
set.Add(0);
set.Add(-50);
set.Add(10);
Console.WriteLine("Set contains 12 -> {0}", set.Find(12));
Console.WriteLine("Set contains 13 -> {0}", set.Find(13));
set.Remove(10);
Console.WriteLine("Removed 10\nSet contains 10 -> {0}", set.Find(10));
Console.WriteLine("Set contains {0} items", set.Count);
Console.WriteLine("Set 1: {0}", set);
var anotherSet = new HashedSet<int>();
anotherSet.Add(-4);
anotherSet.Add(15);
anotherSet.Add(0);
anotherSet.Add(-122);
anotherSet.Add(35);
Console.WriteLine("Set 2: {0}", anotherSet);
set.Union(anotherSet);
Console.WriteLine("Set after union: {0}", set);
set.Intersect(anotherSet);
Console.WriteLine("Set after intersect: {0}", set);
set.Clear();
Console.WriteLine("Set contains {0} items after clear", set.Count);
}
示例2: Main
public static void Main()
{
HashedSet<float> firstSet = new HashedSet<float>();
firstSet.Add(1f);
firstSet.Add(1.4f);
firstSet.Add(1.7f);
firstSet.Add(2f);
firstSet.Add(2.2f);
firstSet.Remove(1.7f);
Console.WriteLine(firstSet.Find(1f));
Console.WriteLine(firstSet.Count);
HashedSet<float> secondSet = new HashedSet<float>();
secondSet.Add(1f);
secondSet.Add(2f);
secondSet.Add(3f);
secondSet.Add(5f);
HashedSet<float> thirdSet = new HashedSet<float>();
thirdSet.Add(1f);
thirdSet.Add(2f);
thirdSet.Add(3f);
thirdSet.Add(5f);
secondSet.Union(firstSet);
thirdSet.Intersect(firstSet);
firstSet.Clear();
}
示例3: TestClearShouldProperlyWork
public void TestClearShouldProperlyWork()
{
var table = new HashedSet<string>();
table.Add("Pesho");
table.Clear();
Assert.AreEqual(0, table.Count);
Assert.AreEqual(16, table.Capacity);
}
示例4: ClearShouldResultInKeysCountOfZeroAndCountOfZero
public void ClearShouldResultInKeysCountOfZeroAndCountOfZero()
{
var set = new HashedSet<int>();
int value = 5;
set.Add(value);
set.Clear();
bool expected = true;
Assert.AreEqual(expected, set.Count == 0);
}
示例5: ShouldEmtyTheSet
public void ShouldEmtyTheSet()
{
var hashedSet = new HashedSet<string>();
for (int i = 0; i < 10; i++)
{
hashedSet.Add("test" + i);
}
hashedSet.Clear();
Assert.AreEqual(0, hashedSet.Count);
}
示例6: ClearShouldWorkCorrectly
public void ClearShouldWorkCorrectly()
{
var set = new HashedSet<string>();
for (int i = 0; i < 20; i++)
{
set.Add(string.Format("pesho #{0}", i % 5));
}
set.Clear();
Assert.AreEqual(0, set.Count);
for (int i = 0; i < 4; i++)
{
Assert.IsFalse(set.Contains(string.Format("pesho #{0}", i)));
}
}
示例7: Main
internal static void Main()
{
HashedSet<int> test = new HashedSet<int>();
HashedSet<int> other = new HashedSet<int>();
test.Add(1);
test.Add(2);
test.Add(3);
test.Add(4);
test.Add(5);
test.Add(6);
other.Add(4);
other.Add(5);
other.Add(6);
other.Add(7);
other.Add(8);
Console.WriteLine("Initial hash set:");
Console.WriteLine(string.Join(", ", test));
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("After removal of 3:");
test.Remove(3);
Console.WriteLine(string.Join(", ", test));
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Is 1 found? {0}", test.Find(1));
Console.WriteLine("Is 3 found? {0}", test.Find(3));
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("First hash set:");
Console.WriteLine(string.Join(", ", test));
Console.WriteLine("Member count: {0}", test.Count);
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Second hash set:");
Console.WriteLine(string.Join(", ", other));
Console.WriteLine("Member count: {0}", other.Count);
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Intersect of the first and second:");
Console.WriteLine(string.Join(", ", test.Intersect(other)));
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Union of the first and second:");
Console.WriteLine(string.Join(", ", test.Union(other)));
Console.WriteLine("--------------------------------------------------------------");
test.Clear();
Console.WriteLine("First hash set after clear:");
Console.WriteLine(string.Join(", ", test));
}
示例8: Main
static void Main()
{
HashedSet<int> hSet = new HashedSet<int>();
hSet.Add(5);
hSet.Add(15);
hSet.Add(5);
Console.WriteLine(hSet.Find(45));
Console.WriteLine(hSet.Find(15));
hSet.Remove(5);
Console.WriteLine(hSet.Find(5));
hSet.Clear();
Console.WriteLine(hSet.Count);
hSet.Add(5);
hSet.Add(15);
hSet.Add(25);
hSet.Add(35);
HashedSet<int> hSetTwo = new HashedSet<int>();
hSetTwo.Add(4);
hSetTwo.Add(24);
hSetTwo.Add(25);
hSetTwo.Add(35);
var newIntersectedSet = hSet.Intersect(hSetTwo);
foreach (var item in newIntersectedSet.Keys)
{
Console.WriteLine(item);
}
var newUnitedSet = hSet.Union(hSetTwo);
foreach (var item in newUnitedSet.Keys)
{
Console.WriteLine(item);
}
}
示例9: Main
static void Main()
{
HashedSet<int> hashSet1 = new HashedSet<int>();
for (int i = 0; i < 10; i++)
{
hashSet1.Add(i);
}
Console.WriteLine("Find element with key = 4");
Console.WriteLine(hashSet1.Find(4));
Console.WriteLine("Find element with key = 2 and write the count of elements");
hashSet1.Remove(2);
Console.WriteLine(hashSet1.Count);
HashedSet<int> hashSet2 = new HashedSet<int>();
hashSet2.Add(5);
hashSet2.Add(9);
hashSet2.Add(33);
Console.WriteLine("Union: ");
hashSet1.UnionWith(hashSet2);
for (int i = 0; i < hashSet1.Container.Count; i++)
{
Console.WriteLine(hashSet1.Container.Keys[i]);
}
Console.WriteLine();
Console.WriteLine("Intersect: ");
hashSet1.IntersectWith(hashSet2);
for (int i = 0; i < hashSet1.Container.Count; i++)
{
Console.WriteLine(hashSet1.Container.Keys[i]);
}
Console.WriteLine();
Console.WriteLine("count after clear");
hashSet2.Clear();
Console.WriteLine(hashSet2.Container.Count);
}
示例10: Main
/* Task 5:
* Implement the data structure "set" in a class HashedSet<T> using your class HashTable<K,T>
* to hold the elements. Implement all standard set operations like Add(T), Find(T), Remove(T),
* Count, Clear(), union and intersect.
*/
static void Main(string[] args)
{
var setOne = new HashedSet<int>();
for (int i = 0; i < 50; i++)
{
setOne.Add(i);
}
Console.WriteLine(setOne.Count());
for (int i = 25; i < 50; i++)
{
setOne.Remove(i);
}
Console.WriteLine(setOne.Count());
var setTwo = new HashedSet<int>();
setTwo.Add(100);
setTwo.Add(101);
setTwo.Add(102);
setTwo.Add(103);
setTwo.Add(104);
setTwo.Add(105);
//Saving all results from Union
setOne = setOne.Union(setTwo);
Console.WriteLine(setOne.Count());
setTwo.Clear();
for (int i = 10; i < 25; i++)
{
setTwo.Add(i);
}
//Saving only the intersect Results
setOne = setOne.Intersect(setTwo);
Console.WriteLine(setOne.Count());
}
示例11: Main
public static void Main()
{
// Input array that contains three duplicate strings.
string[] arrayWithDuplicatedValues = { "cat", "dog", "cat", "leopard", "tiger", "cat" };
// Display the array.
Console.WriteLine("---------- Array with duplicated items ----------");
Console.WriteLine(string.Join(",", arrayWithDuplicatedValues));
// Use HashSet constructor to ensure unique strings.
var hashedSet = new HashedSet<string>();
for (int i = 0; i < arrayWithDuplicatedValues.Length; i++)
{
hashedSet.Add(arrayWithDuplicatedValues[i]);
}
// Display the resulting array.
Console.WriteLine("\n---------- HashedSet without duplicated items ----------");
foreach (var item in hashedSet)
{
Console.WriteLine(item);
}
Console.WriteLine("\n---------- Test Find(cat) ----------");
Console.WriteLine(hashedSet.Find("cat"));
Console.WriteLine("\n---------- Test Remove(cat) and print all elements again ----------");
hashedSet.Remove("cat");
foreach (var item in hashedSet)
{
Console.WriteLine(item);
}
Console.WriteLine("\n---------- Test Count() ----------");
Console.WriteLine(hashedSet.Count());
Console.WriteLine("\n---------- Test Clear() and print count ----------");
hashedSet.Clear();
Console.WriteLine(hashedSet.Count());
}
示例12: Main
static void Main(string[] args)
{
var set = new HashedSet<int>(new[] { 1, 2, 3, 4, 5 });
Console.WriteLine("Set: {0}", set);
// Add
set.Add(6);
Console.WriteLine("Add '6' -> {0}", set);
// Add existing
set.Add(6);
Console.WriteLine("Add '6' -> {0}", set);
// Find
Console.WriteLine("Find '0': {0}", set.Find(0));
Console.WriteLine("Find '6': {0}", set.Find(6));
// Remove
set.Remove(3);
Console.WriteLine("Remove '3' -> {0}", set);
// Count
Console.WriteLine("Number of elements: {0}", set.Count);
// Union
var collection = new[] { 3, 7 };
set.Union(collection);
Console.WriteLine("Union with [{0}] -> {1}", string.Join(", ", collection), set);
// Intersection
collection = new[] { 5, 6, 7, 8, 9 };
set.Intersect(collection);
Console.WriteLine("Intersection with [{0}] -> {1}", string.Join(", ", collection), set);
// Clear
set.Clear();
Console.WriteLine("Clear set...");
Console.WriteLine("Number of elements: {0}", set.Count);
}
示例13: TestClearMethod
public void TestClearMethod()
{
var hashedSet = new HashedSet<int>();
Assert.IsTrue(hashedSet.Add(1));
Assert.IsTrue(hashedSet.Add(2));
Assert.IsTrue(hashedSet.Add(3));
Assert.IsTrue(hashedSet.Add(4));
Assert.AreEqual(4, hashedSet.Count);
hashedSet.Clear();
Assert.AreEqual(0, hashedSet.Count);
Assert.IsFalse(hashedSet.Contains(1));
Assert.IsFalse(hashedSet.Contains(2));
Assert.IsTrue(hashedSet.Add(3));
Assert.IsTrue(hashedSet.Add(4));
}
示例14: CountAndClear
public void CountAndClear()
{
HashedSet<string> set1 = new HashedSet<string>(
StringComparer.InvariantCultureIgnoreCase);
Assert.AreEqual(0, set1.Count);
set1.Add("hello"); Assert.AreEqual(1, set1.Count);
set1.Add("foo"); Assert.AreEqual(2, set1.Count);
set1.Add(""); Assert.AreEqual(3, set1.Count);
set1.Add("HELLO"); Assert.AreEqual(3, set1.Count);
set1.Add("foo"); Assert.AreEqual(3, set1.Count);
set1.Add("Hello"); Assert.AreEqual(3, set1.Count);
set1.Add("Eric"); Assert.AreEqual(4, set1.Count);
set1.Clear();
Assert.AreEqual(0, set1.Count);
bool found = false;
foreach (string s in set1)
found = true;
Assert.IsFalse(found);
}
示例15: Remove
public void Remove()
{
HashedSet<string> set1 = new HashedSet<string>(
StringComparer.InvariantCultureIgnoreCase);
bool b;
b = set1.Remove("Eric"); Assert.IsFalse(b);
b = set1.Add("hello"); Assert.IsTrue(b);
b = set1.Add("foo"); Assert.IsTrue(b);
b = set1.Add(""); Assert.IsTrue(b);
b = set1.Remove("HELLO"); Assert.IsTrue(b);
b = set1.Remove("hello"); Assert.IsFalse(b);
b = set1.Add("Hello"); Assert.IsTrue(b);
b = set1.Add("Eric"); Assert.IsTrue(b);
b = set1.Add("Eric"); Assert.IsFalse(b);
b = set1.Remove("eRic"); Assert.IsTrue(b);
b = set1.Remove("eRic"); Assert.IsFalse(b);
set1.Clear();
b = set1.Remove(""); Assert.IsFalse(b);
}