[英]Turn string[] into List<KeyValuePair<int, string>>
I have this strig[] : 我有这个strig []:
string[] SkippedAreasArray = new string[] {"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"};
I want to turn this into : 我想把它变成:
List<KeyValuePair<int, string>> SkippedAreasArray = ???
I haven't been working with this List<KeyValuePair<int, string>>
data structure. 我没有使用过这个
List<KeyValuePair<int, string>>
数据结构。 So I have to questions how would my string array looked like if I turn it to List<KeyValuePair<int, string>>
(how to define it?). 所以我不得不质疑我的字符串数组如果将其转换为
List<KeyValuePair<int, string>>
(如何定义它?)。 And also - my logic is heavily depending on the indexes that are easily taken when I work with simple string[]
. 而且 - 我的逻辑很大程度上取决于我使用简单的
string[]
时容易采用的索引。 The idea to use List<KeyValuePair<int, string>>
is that I may need to use values like "A1"
, "B1"
, "B2"
and I understand List<KeyValuePair<int, string>>
is the better data structure to easily add those when I need but can I keep the correct indexing of the elements? 使用
List<KeyValuePair<int, string>>
的想法是我可能需要使用像"A1"
, "B1"
, "B2"
,我理解List<KeyValuePair<int, string>>
是更好的数据结构在我需要的时候轻松添加那些但我可以保持元素的正确索引吗?
Is this what you are looking for: 这是你想要的:
string[] SkippedAreasArray = new string[] {"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"};
Dictionary<int,string> dictionary =
SkippedAreasArray.Select((r, i) => new { value = r, index = i })
.ToDictionary(t => t.index, t => t.value);
where key
is the index and value
is the alphabet. 其中
key
是索引, value
是字母表。
OR 要么
List<KeyValuePair<int, string>> list =
SkippedAreasArray.Select((r, index) => new KeyValuePair<int,string>(index, r))
.ToList()
; ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.