Reputation: 3854
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]);
}
Upvotes: 0
Views: 2297
Reputation: 286
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.
Upvotes: 1
Reputation: 66
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.
Upvotes: 1
Reputation: 7304
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.
Upvotes: 0
Reputation: 734
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();
}
Upvotes: 2
Reputation: 1770
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))));
Upvotes: 3
Reputation: 1183
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.
Upvotes: 0
Reputation: 21855
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);
}
}
Upvotes: 0