List<string> lst = new List<string>() { "mahdi","arshia","amir"};
int a = 0;
var list_mian = lst[a];
for (int i = a; i <Convert.ToInt16(list_mian); i ) //Additional information: Input string was not in a correct format.
{
MessageBox.Show(lst.IndexOf(lst[0]).ToString());
}
我想在回圈中顯示串列項的索引,例如 mahdi 的索引是 0,amir 的索引是 2 我想在“for”回圈中分別顯示它們的索引,我給出了一個錯誤,我在代碼部分顯示
uj5u.com熱心網友回復:
您嘗試將整數轉換為字串,然后將其用作 for 回圈上的范圍,只需使用 .count 并將其與附加到串列索引的名稱進行比較。希望您覺得這個有幫助。
public static int? findPerson(string name)
{
List<string> lst = new List<string>() { "mahdi", "arshia", "amir" };
int? result = null;
for (int i = 0; i < lst.Count; i ) //Additional information: Input string was not in a correct format.
{
if (lst[i] == name)
{
result = i;
}
}
return result;
}
static void Main(string[] args)
{
var index = findPerson("arshia");
if (index == null)
{
Console.WriteLine("PersonNotFound");
}
else {
Console.WriteLine("Index of " index.ToString());
}
}
uj5u.com熱心網友回復:
您可以使用IndexOf 來完成它回傳索引或 -1 當沒有專案時。
List<string> list = new List<string>() { "mahdi", "arshia", "amir" };
var indexOfAmir = list.IndexOf("amir"); // 2
var indexOfMax = list.IndexOf("max"); // -1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347532.html
上一篇:Pyspark一般匯入問題
