I have a problem here and it seems that my brain has just left the building so I need you guys to help me out. I have an API - method which requires a multidimensional string array. It looks like this:
string[,] specialMacroArray = new string[,] { { "#macro#", "text1" }, {"#secondmacro#", "text2"} }
The contents of the array are calculated throughout my method and therefor I cannot write it like above. So I put the values in a List throughout my code like this:
List<string[]> specialMacros = new List<string[]>();
specialMacros.Add(new string[] { "#macro#", text1 });
specialMacros.Add(new string[] { "#secondmacro#", "text2" });
So far so good... but now I want to convert the list to the multidimensional array. But I can't figure out how.
specialMacroArray = specialMacros.ToArray()
I am using the .NET 3.5 Framework in C#
Thanx in advance
For this case you could just do this:
specialMacroArray = new string[specialMacros.Count, 2];
for (int i = 0; i < specialMacros.Count; i++)
{
specialMacroArray[i, 0] = specialMacros[i][0];
specialMacroArray[i, 1] = specialMacros[i][1];
}
You'll probably have to do it manually. Something like this:
string[,] array = new string[specialMacroArray.Count, 2]
for (int i=0; i<specialMacroArray.Count; ++i)
{
array[i, 0] = specialMacroArray[i][0];
array[i, 1] = specialMacroArray[i][1];
}
This should help you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sandbox
{
class Program
{
static void Main(string[] args)
{
List<string[]> specialMacros = new List<string[]>();
specialMacros.Add(new string[] { "#macro#", "text1" });
specialMacros.Add(new string[] { "#secondmacro#", "text2" });
var op = specialMacros.ToMultiDimensionalArray();
Console.Read();
}
}
public static class ArrayHelper
{
public static string[,] ToMultiDimensionalArray(this List<string[]> dt)
{
int col = dt.FirstOrDefault().ToList().Count();
string[,] arr = new string[dt.Count, col];
int r = 0;
foreach (string[] dr in dt)
{
for (int c = 0; c < col; c++)
{
arr[r, c] = dr[c];
}
r++;
}
return arr;
}
}
}
Based on the some of comments I have edited the function but I believe this user can make his own judgement and improve the code if they need to.
public static string[,] ToMultiDimensionalArray(this List<string[]> dt)
{
if (dt.Count == 0 )
throw new ArgumentException("Input arg has no elemets");
int col = dt[0].Count();
string[,] arr = new string[dt.Count, col];
int r = 0;
foreach (string[] dr in dt)
{
for (int c = 0; c < col; c++)
{
arr[r, c] = dr[c];
}
r++;
}
return arr;
}
string[][] specialMacroArray = new string[][] { new string[] { "#macro#", "text1" }, new string[] { "#secondmacro#", "text2" } };
List<string[]> specialMacros = new List<string[]>();
specialMacros.Add(new string[] { "#macro1#", "text1" });
specialMacros.Add(new string[] { "#secondmacro#", "text2" });
specialMacroArray = specialMacros.ToArray();
works fine, no loop is needed... all u have to do is change string[,]
to string[][]
(that's array of array, initializes in a bit diff way)
updated code:
List<string[,]> specialMacros = new List<string[,]>();
specialMacros.Add(new string[,] { { "#secondmacro#", "text2" }, { "#secondmacro#", "text2" } });
specialMacroArray =specialMacros[0];
精彩评论