问题
Related, but not Duplicate (it's a completely different language, PHP not C#):
How to create multi-dimensional array from a list?
How would I go about converting a list of 'Tuple's to string[,]?
This is part of a web crawler, but i'm messing with conversions of lists and arrays just out of curiosity. Here's the method.
private string[,] getimages(string url)
{
List<Tuple<string, string>> images = new List<Tuple<string, string>>();
string raw = client.DownloadString(url);
while (raw.Contains("<a class=\"title \" href"))
{
raw = raw.Substring(raw.IndexOf("<a class=\"title \" href"));
String link = raw.Substring(24, raw.IndexOf(">", 24) - 26);
int startname = raw.IndexOf(">", 24) + 1;
int endname = raw.IndexOf("</a> ");
String name = raw.Substring(startname, endname - startname);
images.Add(new Tuple<string, string>(name, link));
raw = raw.Substring(endname);
}
}
I want to return 'images', but converted to a multidimensional array.
回答1:
A simple and straight-forward way is to just for
the list:
string[,] result = new string[images.Count, 2];
for(int i=0; i<images.Count; i++)
{
var tuple = images[i];
result[i,0] = tuple.Item1;
result[i,1] = tuple.Item2;
}
return result;
来源:https://stackoverflow.com/questions/13982940/list-of-tuples-to-multi-dimensional-array