我正在尋找幫助在策略上重命名列表中的字母數字字符串同時保持索引。保留索引時重命名列表中的字符串部分
我維護幾個並行列表到下面的代碼中提到的missionList,所以索引是重要的。當用戶刪除列表中的一個條目時,就會出現問題,這會在同名的任務類型之間留下空隙。
從{Mapping1,Mapping2,Mapping3}刪除Mapping2 葉{Mapping1,Mapping3}但我想它是{Mapping1, Mapping2}。
在添加其他任務類型(如侵蝕和犯罪)之前,這並不困難。有一個未定數量的任務類型,所以代碼需要爲此做好準備。
{Mapping1,Mapping3,Erosion2}的重命名操作應在{Mapping1,Mapping2,Erosion1}中產生 。
我添加了下面的代碼,爲此設置了代碼。
static void Main(string[] args)
{
List<string> missionList = new List<string> { "Mapping1", "Mapping3", "Erosion2",
"Mapping4", "Erosion3", "Crime1", "Mapping6", "Mapping8", "Erosion1" };
for (int i = 0; i < missionList.Count; i++)
{
// Get the mission number
int firstNumber = missionList[i].IndexOfAny("".ToCharArray());
int number = Convert.ToInt32(missionList[i].Substring(firstNumber));
// Get the mission name
string name = missionList[i].Substring(0, firstNumber);
//TODO: Rename missionList strings
// - Index of each string remains the same
// - Each "name" portion of the string stays the same
// - The "number" portion of the string is changed to reflect its position
// in relation to items with the same "name"
// ex. { "Mapping4", "Mapping3" } becomes { "Mapping1", "Mapping2" }
}
/* After remaming, missionList should be:
{ "Mapping1", "Mapping2", "Erosion1", "Mapping3", "Erosion2", "Crime1",
"Mapping4", "Mapping5", "Erosion3" } */
}
也許字符串列表是不是最好的數據結構爲你」重新努力完成。 –