0

I have a 2d string array (at least I think it is called a 2d array):

var target = new string[var1,var2];

Now I want to convert it to List<List<string>>:

var listlist = new List<List<string>>();
foreach (var row in target)
{
  var newlist = new List<string>();
  foreach (var el in row)
  {
    newlist.Add(el);
  }
  listlist.Add(newlist);
}

But row has a type is string and el has type is char.

I can't understand why el is not a string? What's wrong?

2
  • 2
    When you enumerate a string[], you get string. When you enumerate a string, you get char because it implements IEnumerable<char>. What point didn't you understand? Commented Jan 6, 2015 at 13:19
  • 1
    If this behavior is correct so how can i create a 2d string array? Commented Jan 6, 2015 at 13:21

6 Answers 6

5

A foreach interates over a string[,] like it is a string[]. It doesn't split in rows.

If you do want to handle 'rows' and 'columns' those separately, you have to get the dimensions of the array, using the GetLength method:

var target = new string[var1, var2];

var listlist = new List<List<string>>();
for (int x = 0; x < target.GetLength(0); x++)
{
    var newlist = new List<string>();
    for (int y = 0; y < target.GetLength(1); y++)
    {
        newlist.Add(target[x, y]);
    }
    listlist.Add(newlist);
}
Sign up to request clarification or add additional context in comments.

Comments

2

This is what you need

    static void SoStrList()
    {
        int var1=10, var2=7;
        var target=new string[var1, var2];

        var listlist=new List<List<string>>();

        for(int i=0; i<var1; i++)
        {
            var row=new List<string>();
            for(int j=0; j<var2; j++)
            {
                row.Add(target[i, j]);
            }
            listlist.Add(row);
        }
    }

Comments

1

use for loop instead of foreach

        var target = new string[2, 2];
        target[0, 0] = "a";
        target[0, 1] = "A";
        target[1, 0] = "b";
        target[1, 1] = "B";

        var listlist = new List<List<string>>();

        for (int i = 0; i < target.GetLength(0); i++)
        {
            var newlist = new List<string>();

            for (int j = 0; j < target.GetLength(1); j++)
                newlist.Add(target[i,j]);

            listlist.Add(newlist);
        }

Comments

0

Here:

foreach (var row in target)

You already have first element of your 2d array, and foreach take all chars of this elements

Comments

0

well ... string is array of chars. And this confusion is what you get from using keyword var for almost everything. Its not javascript.

Secondly : You need to go for something like this

for (int i = 0; i < target.GetLength(0); i++)
        {
            for (int y = 0; y < target.GetLength(1); y++)
            {
                your manipulation with strings
            }
        }

but srsly... get rid of vars !!!

Comments

0

For LINQ lovers, the same can be achieved using the following two lines:

int R = s.GetLength(0), C = s.GetLength(1);
var MyList = Enumerable.Range(0, R).Select(i => i * C).Select(i => s.Cast<string>.Skip(i).Take(C).ToList()).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.