【问题标题】:How to: Remove an item from a List<string>如何:从 List<string> 中删除项目
【发布时间】:2014-07-20 04:17:29
【问题描述】:

如何:从列表中删除项目

我有以下代码sn-p...

companies.Remove(listView_Test.SelectedItem.ToString());

有一个 listView 包含(比方说)3 个没有名称的项目,只有一个 Content 由“A”、“B”和“C”组成。现在,当我选择该listView 中的一个项目时,我第二次单击一个按钮,该按钮运行我的包含Remove()/RemoveAt() 的方法。现在我想删除List&lt;string&gt; myList的行,该行与所选项目的Content相同。

编辑:Flow Flow OverFlow的解决方案

int index = companies.IndexOf(companyContent);
companies.RemoveAt(index);

【问题讨论】:

  • 调用 .RemoveAt 方法,然后将要删除的项目的索引作为参数传递
  • 您能举个例子说明您要删除的内容吗?
  • 你需要使用 Remove 函数并传入一个谓词。像 x => x.Text == listView_Test.SElectedItem.ToString() 这样的东西。请注意,此代码可能不起作用(这就是我没有将其作为答案的原因)它应该只会引导您朝着正确的方向前进。我现在没有时间发布完整的答案,抱歉

标签: c# string list listview selecteditem


【解决方案1】:

您可以通过项目的已知位置或项目中的内容删除项目。

public static void Main()
{
    List<Object> items = new List<Object>();
    items.Add("test1");
    items.Add("test2");
    items.Add("test3");

foreach(var a in items)
    Console.WriteLine(a.ToString());
Console.WriteLine("--");

items.RemoveAt(1); // remove object at position 1, in this case "test2"

foreach(var a in items)
    Console.WriteLine(a.ToString());
Console.WriteLine("--");

items.RemoveAll(x => ((string) x) == "test1"); // LAMBDA query to remove by a condition

foreach(var a in items)
    Console.WriteLine(a.ToString());
}

输出

test1
test2
test3
--
test1
test3
--
test3

【讨论】:

  • 我认为你也必须覆盖 RemoveRange
【解决方案2】:
public int FindItem(List<string> haystack, string needle)
{ for (int i = 0; i < haystack.Count; i++) 
      if (haystack[i] == needle) return i;
  return -1;
}


try {
     companies.Remove(FindItem(companies, listView_Test.SelectedItem.ToString() ) );
    } catch {  /* not found, no problem.. */ } 

【讨论】:

    【解决方案3】:

    我真的不明白你的问题是什么,但这里有一些可能对你有帮助的参考资料:

    如果您的问题与访问当前选定列表视图项的文本有关,请参见 ListViewItem 的 Text 属性: http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.text(v=vs.110).aspx

    如果您的问题与从通用列表中删除元素有关,请参阅 List.Remove:http://msdn.microsoft.com/en-us/library/cd666k3e(v=vs.110).aspx

    【讨论】:

      【解决方案4】:

      您必须获取要从列表中删除的对象的索引,然后您可以:

      //Assuming companies is a list 
      companies.RemoveAt(i);
      

      要获取项目的索引,您可以使用:

      companies.IndexOf("Item");
      

      或使用带有条件语句的 for 循环:

      for (int i = 0; i < companies.Count; i++) {
         // if it is List<String>
         if (companies[i].equals("Something")) {
               companies.RemoveAt(i);   
         }
      }
      

      【讨论】:

      • “对象的索引”是什么意思?如果你是这个意思,我不知道它的立场。它在List&lt;string&gt; 的某个地方,我只知道“名称”。这就像“从 myList 中删除 * Content = 'Hello World'”。
      • 谢谢,这就是我想做的。
      • 如果您在迭代过程中删除一个项目,列表大小/计数会发生变化,i &lt; companies.Count 条件并不能保证检查所有列表。请尝试列出两个可移动项目:{"Something","Something"}
      • @Tobia 那么,以倒序遍历列表会更好吗,比如for (int i=(companies.Count-1); i&gt;=0; i--)
      • 是的,这可以是一个解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多