List of Tuples to multi-dimensional array

ε祈祈猫儿з 提交于 2019-12-11 05:52:17

问题


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>&#32;");
            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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!