简体   繁体   English

使用HashSet <int> 创建一个整数集

[英]Using HashSet<int> to create an integer set

I want to create an class that represents an integer set using a HashSet<int> . 我想创建一个使用HashSet<int>表示整数集的类。 I want it to keep track of which values are included in the set using that internal container. 我希望它使用该内部容器跟踪集合中包含的值。 I've done this so far: 到目前为止我做到了这一点:

class SetInteger
{
    HashSet<int> intTest= new HashSet<int>();
    intTest.Add(1);
    intTest.Add(2);
    intTest.Add(3);
    intTest.Add(4);
    intTest.Add(5);
    intTest.Add(6);
    intTest.Add(7);
    intTest.Add(8);
    intTest.Add(9);
    intTest.Add(10);
}

So, here I think I'm adding some values to the HashSet , but I dont see how this can keep track of which values that are included in the set. 所以,在这里我想我正在为HashSet添加一些值,但是我没有看到它如何跟踪集合中包含的值。 Any ideas? 有任何想法吗?

The hash set has a Contains method that allows you to check whether a value is in the set. 哈希集有一个Contains方法,允许您检查值是否在集合中。

In addition, the HashSet<T> implements the ISet<T> interface and therefore provides many methods for working with sets, such as union, intersection and determining if a set of values is a (proper) super- or subset of your set. 此外, HashSet<T>实现了ISet<T>接口,因此提供了许多用于处理集合的方法,例如并集和交集,并确定一组值是否是(适当的)超集或子集。

HashSet<int> intTest = new HashSet<int>()
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

bool has4 = intTest.Contains(4);    // Returns true
bool has11 = intTest.Contains(11);  // Returns false
bool result = intTest.IsSupersetOf(new []{ 4, 6, 7 }); // Returns true

By the way, did you know about the collection initializer syntax? 顺便问一下,你知道集合初始化器的语法吗?


You can also foreach on the set to get each element it contains (in an unspecified order): 你也可以foreach就设置为得到它含有(以未指定的顺序)的每个元素:

foreach(int value in intTest)
{
    // Do something with value.
}

Or convert it to an array or mutable list (also in an unspecified order): 或者将其转换为数组或可变列表(也以未指定的顺序):

int[] arr = intTest.ToArray();
List<int> lst = intTest.ToList();

You can use HashSet Contains method tell's if the value already exists! 如果值已存在,您可以使用HashSet Contains方法告诉我们!

Example : 示例:

if (intTest.Contains(5))
{
    // already has the value
}

Hmm...well, a HashSet<T> implements IEnumerable<T> , so you can always do this to figure out "What's already in there": 嗯......好吧, HashSet<T>实现了IEnumerable<T> ,所以你总是可以做到这一点来弄清楚“那里已有什么”:

HashSet<int> intTest= new HashSet<int>();
intTest.Add(1);
intTest.Add(2);
intTest.Add(3);
intTest.Add(4);
intTest.Add(5);
intTest.Add(6);
intTest.Add(7);
intTest.Add(8);
intTest.Add(9);
intTest.Add(10);
var inThereNow = intTest.ToArray();  // [1,2,3,4,5,6,7,8,9,10]

There's also bool Contains(T value) which will tell you if a specific value is in the set, IEnumerable<T> Union(IEnumerable<T> other) which will tell you the "OR" of two sets, IEnumerable<T> Intersect(IEnumerable<T> other) which will tell you the overlap of two sets...pretty much anything in either IEnumerable<T> or ISet<T> 还有bool Contains(T value) ,它会告诉你特定值是否在集合中, IEnumerable<T> Union(IEnumerable<T> other)会告诉你两个集合的“OR”, IEnumerable<T> Intersect(IEnumerable<T> other)会告诉你两个集合的重叠...... IEnumerable<T>ISet<T>任何内容都是ISet<T>

Us the Contains method: http://msdn.microsoft.com/en-us/library/bb356440.aspx 我们的Contains方法: http//msdn.microsoft.com/en-us/library/bb356440.aspx

Hope this helps. 希望这可以帮助。

you can try this. 你可以试试这个。 you just take a one textbox and two button. 你只需要一个文本框和两个按钮。

HashSet<int> hs = new HashSet<int>();
    private void savedataonhashSet_Click(object sender, EventArgs e)
    {

        hs.Add(Convert.ToInt16(textBox1.Text));
   }

    private void checkduplicatevalue_Click(object sender, EventArgs e)
    {
        if (hs.Contains(00))          

        {
            MessageBox.Show("it is");
        }
        else
        {
            MessageBox.Show("not there");
        }
    }

if you again problem faced just drop your code ..... 如果你再次遇到问题只需丢弃你的代码.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM