2017-03-02 21 views
1

我正在尋找幫助在策略上重命名列表中的字母數字字符串同時保持索引。保留索引時重命名列表中的字符串部分

我維護幾個並行列表到下面的代碼中提到的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" } */ 
    } 
+0

也許字符串列表是不是最好的數據結構爲你」重新努力完成。 –

回答

1

像這樣的東西可以工作

using System.Collections.Generic; 
using System.Text.RegularExpressions; 
using System.Linq; 

    private static void DoIt() 
    { 
     Regex regex = new Regex("\\d*", RegexOptions.None);//we'll use this ro remove the existing numbers 
     List<string> thelista = new List<string>() { "aa11", "ab2", "aa4", "df4" };//lets fake a list 
     List<string> thelist = new List<string>() { "Mapping1", "Mapping3", "Erosion2", "Mapping4", "Erosion3", "Crime1", "Mapping6", "Mapping8", "Erosion1"};//Lets fake your list 

     List<string> templist = new List<string>();//our temp storage 
     Dictionary<string, int> counter = new Dictionary<string, int>();//our counting mechanism 


     for (int i = 0; i < thelist.Count; i++)//loop through the original list of string 
     { 
      templist.Add(regex.Replace(thelist[i], ""));//strip the number and add it to the temp list 

      if (!counter.ContainsKey(templist.Last())) 
      { 
       counter.Add(templist.Last(), 0);//add the type to the counter dictionnary and set the "counter" to 0 
      } 

     } 

     for (int i = 0; i < templist.Count; i++)//loop through the temp list 
     { 
      counter[templist[i]]++;//increment the counter of the proper type 
      templist[i] = templist[i] + counter[templist[i]];//add the counter value to the string in the list 
     } 

     thelist = templist;//tadam 

    } 

輸入

Mapping1 
Mapping3 
Erosion2 
Mapping4 
Erosion3 
Crime1 
Mapping6 
Mapping8 
Erosion1 

輸出

Mapping1 
Mapping2 
Erosion1 
Mapping3 
Erosion2 
Crime1 
Mapping4 
Mapping5 
Erosion3 
+0

工作就像一個魅力!謝謝! – Surfinsanta

相關問題