使用List ForEach将元素添加到HashTable

时间:2021-05-21 21:19:21

I have a list of string values that I want add to a hashtable or other array that can be accessed by key/index but cannot implement it. I have this working how I want but its ugly

我有一个字符串值列表,我想添加到哈希表或其他数组,可以通过键/索引访问但不能实现它。我有这个工作我想要但它的丑陋

        List<string> valueList = new List<string>();
        valueList.Add("1");
        valueList.Add("2");
        valueList.Add("3");

        Hashtable p = new Hashtable();


        valueList.ForEach(delegate(string f) { p.Add(valueList.FindIndex(v => v == f), f); });

EDIT: After James reminded me that a List will return values by index I went with a List and this is what I have ended up with

编辑:在詹姆斯提醒我列表将按索引返回值后,我选择了List,这就是我最终的结果

valueList.ForEach(f => sequenceList.Add(int.Parse(f)));

5 个解决方案

#1


Try this:

valueList.ForEach(x => htable.Add(valueList.FindIndex(y => y == x), x));

Although, there's really no reason not to use a for here

虽然,实际上没有理由不在这里使用

for (var index = 0; index < valueList.Count; index++)
{
    htable.Add(index, valueList[index]);
}

It's more lines of code, but it's more straightforward and will perform much better (findIndex is far less efficient than using the index from the for statement).

这是更多的代码行,但它更直接,性能更好(findIndex的效率远低于使用for语句中的索引)。

#2


Even if it compiled, it wouldn't work - calling GetEnumerator().Current will always fail because it'll give you a new iterator positioned before the first item.

即使它被编译,它也行不通 - 调用GetEnumerator()。当前将永远失败,因为它会给你一个位于第一个项目之前的新迭代器。

What do you want the key for each item to be? If it's just its position within the list, I don't see the benefit that gives you over a list (which is already indexed by int). However, you can do it like this (assuming that valueList is a List<string>:

你想要每件物品的钥匙是什么?如果它只是它在列表中的位置,我看不到给你一个列表的好处(已经被int索引)。但是,您可以这样做(假设valueList是List :

var dictionary = valueList.Select((item, index) => new { item, index })
                          .ToDictionary(x => x.index, x => x.item);

Adding it to an existing dictionary, I'd just do:

将它添加到现有字典中,我只是这样做:

for (int i=0; i < valueList.Count; i++)
{
    dictionary[i] = valueList[i];
}

Not everything has to be done with lambdas :)

不是所有事情都必须用lambdas完成:)

Note that this won't have quite the same effect as using FindIndex if you have repeated values.

请注意,如果您有重复的值,这与使用FindIndex的效果不会完全相同。

#3


Assuming you want the strings to be the keys and the index to be the value:

假设您希望字符串为键,索引为值:

Hashtable ht = new Hashtable();

for (var i = 0; i <= valueList.Count; i++)
{
    ht.Add(valueList[i], i);
}

Otherwise switch the ht.Add parameters around. However, if that is the case you would be best just to leave it as a List < string >.

否则切换ht.Add参数。但是,如果是这种情况,您最好将其保留为List

#4


List<string> list = new List<string>{"w", "y", "u", "i", "n"};

HashSet<string> hset = new HashSet<string>(list);

#5


Using ForEach for this is not efficient, as you have to use FindIndex to find out where you are. It works, but you will do a lot of extra work:

使用ForEach效率不高,因为您必须使用FindIndex来找出您的位置。它有效,但你会做很多额外的工作:

valueList.ForEach(x => htable.Add(valueList.FindIndex(y => y == x), x));

It's better to just use a regular loop, so that you get the index without having to seek through the table:

最好只使用常规循环,这样就可以获得索引而无需遍历表:

for (int i = 0; i < valueList.Count; i++) {
   htable.Add(i, valueList[i]);
}

#1


Try this:

valueList.ForEach(x => htable.Add(valueList.FindIndex(y => y == x), x));

Although, there's really no reason not to use a for here

虽然,实际上没有理由不在这里使用

for (var index = 0; index < valueList.Count; index++)
{
    htable.Add(index, valueList[index]);
}

It's more lines of code, but it's more straightforward and will perform much better (findIndex is far less efficient than using the index from the for statement).

这是更多的代码行,但它更直接,性能更好(findIndex的效率远低于使用for语句中的索引)。

#2


Even if it compiled, it wouldn't work - calling GetEnumerator().Current will always fail because it'll give you a new iterator positioned before the first item.

即使它被编译,它也行不通 - 调用GetEnumerator()。当前将永远失败,因为它会给你一个位于第一个项目之前的新迭代器。

What do you want the key for each item to be? If it's just its position within the list, I don't see the benefit that gives you over a list (which is already indexed by int). However, you can do it like this (assuming that valueList is a List<string>:

你想要每件物品的钥匙是什么?如果它只是它在列表中的位置,我看不到给你一个列表的好处(已经被int索引)。但是,您可以这样做(假设valueList是List :

var dictionary = valueList.Select((item, index) => new { item, index })
                          .ToDictionary(x => x.index, x => x.item);

Adding it to an existing dictionary, I'd just do:

将它添加到现有字典中,我只是这样做:

for (int i=0; i < valueList.Count; i++)
{
    dictionary[i] = valueList[i];
}

Not everything has to be done with lambdas :)

不是所有事情都必须用lambdas完成:)

Note that this won't have quite the same effect as using FindIndex if you have repeated values.

请注意,如果您有重复的值,这与使用FindIndex的效果不会完全相同。

#3


Assuming you want the strings to be the keys and the index to be the value:

假设您希望字符串为键,索引为值:

Hashtable ht = new Hashtable();

for (var i = 0; i <= valueList.Count; i++)
{
    ht.Add(valueList[i], i);
}

Otherwise switch the ht.Add parameters around. However, if that is the case you would be best just to leave it as a List < string >.

否则切换ht.Add参数。但是,如果是这种情况,您最好将其保留为List

#4


List<string> list = new List<string>{"w", "y", "u", "i", "n"};

HashSet<string> hset = new HashSet<string>(list);

#5


Using ForEach for this is not efficient, as you have to use FindIndex to find out where you are. It works, but you will do a lot of extra work:

使用ForEach效率不高,因为您必须使用FindIndex来找出您的位置。它有效,但你会做很多额外的工作:

valueList.ForEach(x => htable.Add(valueList.FindIndex(y => y == x), x));

It's better to just use a regular loop, so that you get the index without having to seek through the table:

最好只使用常规循环,这样就可以获得索引而无需遍历表:

for (int i = 0; i < valueList.Count; i++) {
   htable.Add(i, valueList[i]);
}