1

i am Using for each Loop to a Jagged String Array in order to display the Elements in it but it is not working and i have tried this code !

public class Program
    {
        public static void Main(string[] args)
        {
            string[][] str = new string[5][];
            str[0] = new string[5];
            str[1] = new string[10];
            str[2] = new string[20];
            str[3] = new string[50];
            str[4] = new string[10];
            //Now store data in the jagged array
            str[0][0] = "Pune";
            str[1][0] = "Kolkata";
            str[2][0] = "Bangalore";
            str[3][0] = "The pink city named Jaipur";
            str[4][0] = "Hyderabad";
            //Lastly, display the content of each of the string arrays inside the jagged array
            foreach(string[] i in str)
                Console.WriteLine(i.ToString());
            Console.ReadKey();
        }
    }

I have used a foreach loop but it is Printing

System.String[]
System.String[]
System.String[]
System.String[]
System.String[]

as output .... So get me the Problem Solution Please as what i have to modify to get the Display as

Pune
Kolkata
Bangalore
The pink city named Jaipur
Hyderabad
1
  • 2
    you should do Console.WriteLine(i[0].ToString()); Commented Mar 22, 2018 at 9:27

6 Answers 6

2

A jagged array is an array of arrays - meaning you must have nested loops:

foreach(string[] i in str)
    foreach(string s in i)
         if(!string.IsNullOrEmpty(s))
             Console.WriteLine(s);

Another option is using linq's SelectMany:

foreach(var s in str.SelectMany(s => s).Where(s => !string.IsNullOrEmpty(s)))
{
    Console.WriteLine(s);
}

Or, if you want to only display the first cell of each nested array:

foreach(string[] i in str)
    var s = i[0];
    if(!string.IsNullOrEmpty(s))
        Console.WriteLine(s);

Note I've added a check for String.IsNullOrEmpty to avoid writing empty lines to the console.

Sign up to request clarification or add additional context in comments.

Comments

1

Try updating your code with this to loop through each array entry in your array :

 foreach(string[] i in str)
 {
     foreach(string s in i)
     {
       Console.WriteLine(s);
     }
 }

Comments

1

Your i itself is a string array and thus the result since Tostring() returning the default implementation of it. You need another loop to get to the real data item

foreach(string[] i in str)
{
  foreach(string j in i)
      Console.WriteLine(j);
}

Comments

1

Jagged array is an array of arrays, so you need a second, nested, loop.

The second loop could be explicit, or hidden inside some method that loops over entries that you pass to it. For example, string.Join would combine all strings from a jagged row into a single string for printing:

foreach(string[] row in str)
    Console.WriteLine(string.Join(" ", row));

You can use the same technique to eliminate the outer loop, too:

Console.WriteLine(string.Join("\n", str.Select(row => string.Join(" ", row))));

1 Comment

Actually, using string.Join is probably the best option. +1.
0

If you want to print all the strings in all the arrays, and not get an exception:

foreach (string[] i in str)
    foreach (string s in i)
        if (s != null)
            Console.WriteLine(s);

Comments

0

Console.WriteLine(i[0].ToString());

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.