0

I have a List of arrays in the given form:

List<string[]> result = new List<string[]>();

Rows of data have been added to it line by line. How would I print each element of the list line by line?

I was able to print certain parts of it by saying the following, but was not able to print everything. Any help will be appreciated!

foreach (var res in result)
   {
       Console.WriteLine(res[2]);
   }
3
  • you are declaring result as List<string[]> result = new List<string[]>(); then from where the world results (var res in results) is coming from??? Commented Jun 10, 2015 at 19:07
  • one liner: Console.WriteLine(string.Join(Environment.NewLine, result.SelectMany(x => x))); Commented Jun 10, 2015 at 19:11
  • 1
    another one: result.ForEach(e => e.ToList().ForEach(Console.WriteLine)); Commented Jun 10, 2015 at 19:11

7 Answers 7

3

If you want to print each string[] on a separate line:

foreach (var res in results)
{
Console.WriteLine(string.Join(",", res));
}

or, cuter:

Console.WriteLine(string.Join(Environment.NewLine, results.Select(x => string.Join(",", x))));
Sign up to request clarification or add additional context in comments.

Comments

2

You want to go through each list and get each array, then go through each string in that array. Then you get the next array from the list and go through each string there as well. Make sense?

Try like this:

foreach (string[] tempArray in result)
{
    foreach(string tempString in tempArray)
    {
        Console.Write(tempstring);
    }
    Console.WriteLine();
}

Comments

1

If you do that you only display the third value in each array, what you need in to iterate over the arrays. You can do it with another loop, or if the array is small(let's say 3 that i am guessing by your example) and the array will always be that size, you can write each value by hand.

With a for loop, it would be like this:

foreach (var res in result)
{
   for(int i=0; i < res.Length;i++){
       Console.WriteLine(res[i]);
   }
}

you can use another foreach loop as sugested by bkribbs:

foreach (var res in result)
{
    foreach(string resText in res)
    {
        Console.WriteLine(resText);
    }
}

Or you can do by hand, wich is not recomended in real life scenarios unless you are aiming for performance. The next example is suposing each array has the same lenght of 3:

foreach (var res in result)
{
    Console.WriteLine(res[0]);
    Console.WriteLine(res[1]);
    Console.WriteLine(res[2]);
}

Always remember that array indexes start at 0, so that if you do something like

int indexes = 3;
int[] array = new int[indexes];

array[indexes] = 200; //the same as array[3] = 200;

it will turn in an error, because it will look for the fourth element in a 3 element array.

Comments

1

You have a list of arrays, so you will have to loop through both of these collections, printing out each of the elements. I suggest doing this by using the following code:

foreach (var res in results)
{
    Console.Write("[");
    for (int i = 0; i < res.Length; i++)
    {
        Console.Write((i == 0 ? "" : ", ") + res[i]);
    }
    Console.WriteLine("]");
}

This will put each array on a new line surrounded by square brackets and the elements of the arrays will be comma delimited.

Comments

0

As CodeCaster said res is an array, so you need to loop over res within the outer foreach with another foreach

foreach (var res in results)
{
    foreach (var otherString in res)
    {
        Console.WriteLine(otherString);
    }
}

Comments

0

Try this:

foreach (var res in result) //is it result or results you have it different 
{
    foreach (var i in res)
    {
        Console.WriteLine(i);
    }
}

The first loop is going through each element in the list and the second loop is going through each element in the array.

Comments

0

The following prints out the strings comma-separated, with one list element per line:

result.ForEach(stArr => Console.WriteLine(string.Join(", ", stArr)));

If you want each string on a new line, just do:

result.ForEach(stArr => Console.WriteLine(string.Join(Environment.NewLine, stArr)));

We're using ForEach to loop through each of the list elements in turn. Each of these is a string array, which is passed to string.Join(). Join just concatenates them, putting the chosen separator after every entry but the last.

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.