Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello guys. I'm new to c# and I have an array like this. i am using for loop to print this array to console but i am getting error. How can I set up an algorithm?


Screenshot 1 - IM.GE - Free Image Hosting - Upload Image & Share[^]
Screenshot 3 - IM.GE - Free Image Hosting - Upload Image & Share[^]

What I have tried:

for (int i = 0; i < arr.Count; i++)
{
Console.WriteLine(i); // 5491
for (int j = 0; j < arr.Count; j++)
{
Console.WriteLine(arr[i][j]);
}
}

Console.ReadLine();
Posted
Updated 1-Sep-21 1:46am

1 solution

You have a list of string arrays, but your inner loop is checking the length of the outer list when it should be checking the length of the inner array.
C#
for (int i = 0; i < arr.Count; i++)
{
    string[] row = arr[i];
    for (int j = 0; j < row.Length; j++)
    {
        Console.WriteLine(row[j]);
    }
}
 
Share this answer
 
Comments
Murkrows 1-Sep-21 8:13am    
Thank you <3
Maciej Los 1-Sep-21 8:15am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900