※ 引述《don750421 (1+1≠2)》之铭言:
: 请问一下,List<string> 与 string[,] 要如何互转呢??
: 问题一: List<string> => string[,]
: 假设有个List<string> 如下
: List<string> lsName = new List<string>();
: lsName.Add("John,Tom,Kevin,Tony");
: lsName.Add("Jane,Candy,Amy,Sandy");
: lsName.Add("Tomas,Tommy,Michael,Mark");
: 希望可以转成以下这种二维阵列
: [0,0][1,0][2,0][3,0]
: [0,1][1,1][2,1][3,1]
: [0,2][1,2][2,2][3,2]
: 除了for 循环外,能够透过LINQ或Lamdba或其他方式做到吗?
: 问题二:
: 假设有个二维阵列如上,有办法在返回成List<string>吗?
: 也就是输出成...
: List<string> lsName = new List<string>()
: {
: "JohnTomKevinTony","JaneCandyAmySandy","TomasTommyMichaelMark"
: };
: 先谢谢各位前辈@@
下班吃饱了 手痒写一下@@"
还请各位前辈指教0rz
问题一:
public void ListToArray()
{
var lsName = new List<string>
{
"John,Tom,Kevin,Tony",
"Jane,Candy,Amy,Sandy",
"Tomas,Tommy,Michael,Mark"
};
var lsNameContainArray = new List<string[]>();
lsName.ForEach(names => {
lsNameContainArray.Add(names.Split(','));
});
var resultArray = lsNameContainArray.ToArray();
Assert.IsType(typeof(string[][]), resultArray);
}
问题二:(再次修正XD)
public void ArrayToList()
{
var nameArray = new[]
{
new []{"John","Tom","Kevin","Tony"},
new []{"Jane","Candy","Amy","Sandy"},
new []{"Tomas","Tommy","Michael","Mark"}
};
var nameList = nameArray.ToList();
var resultList = new List<List<string>>();
nameList.ForEach(names =>
{
resultList.Add(names.ToList());
});
Assert.IsType<List<List<string>>>(resultList);
}