[英]C#: Index was outside the bounds of the array with ListView
我不斷收到“索引超出數組范圍”。 當我嘗試將項目添加到listView時。
我究竟做錯了什么?
這是我的代碼:
string[] h = getBetweenAll(thepage, "\" target=\"_blank\">", "</a>");
foreach (string s in h)
listViewClickbank.Items.Add(new ListViewItem(""));
foreach (ListViewItem i in listViewClickbank.Items)
{
if (i.SubItems[0].Text == "(view mobile)")
{
i.Remove();
}
}
foreach (ListViewItem i in listViewClickbank.Items)
{
if (i.SubItems[0].Text.Contains("recordTitle"))
{
i.Remove();
}
}
string[] u = getBetweenAll(thepage, "<div class=\"description\">", "</div>");
for (int i = 0; i < h.Length && i < listViewClickbank.Items.Count; i++)
{
listViewClickbank.Items[i].SubItems.Add(u[i]);
}
錯誤出現在此行:
listViewClickbank.Items[i].SubItems.Add(u[i]);
請注意,您正在使用h.Length
,而不是u.Length
作為for
循環中的條件。 您要添加u
元素,而不是h
並且很可能是u.Length
小於h.Length
並且在嘗試訪問u [i]時會出現Exception。 它應該是 :
string[] u = getBetweenAll(thepage, "<div class=\"description\">", "</div>");
for (int i = 0; i < u.Length && i < listViewClickbank.Items.Count; i++)
{
listViewClickbank.Items[i].SubItems.Add(u[i]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.