+ 2

I need to populate a jagged array with a string type from the keyboard, how to do it?

I need to populate a jagged array with a string type from the keyboard. how to do it? Like this, only from the keyboard string[][] name = new string[2][]; name[0] = new string[] { "Tom", "John" }; name[1] = new string[] { "Tom", "Gina", "Petya"};

19th Jan 2022, 10:51 AM
Роман Жигунов
Роман Жигунов - avatar
7 ответов
+ 1
Do you mean you want to enter the names dynamically into an array that has prefixed size? You can do it indirectly with list and than cast it back to an array if that's the case. https://code.sololearn.com/cJ7Z07h24j6L/?ref=app
20th Jan 2022, 7:20 AM
Sandy Ho
Sandy Ho - avatar
20th Jan 2022, 12:46 PM
Sandy Ho
Sandy Ho - avatar
+ 1
My code might be writing infinity for the standard compiler. I am using the SoloLearn's simplified compiler for testing so I can't tell. But there's no on in c#, what you need to do is change it to If(input=="exit") The user will need to type exit when done entering the names
20th Jan 2022, 12:58 PM
Sandy Ho
Sandy Ho - avatar
+ 1
Since this is c#, you can make use of dictionary and list to make your array dynamic and more easy to access and maintain. It looks like you are coding this in a c++ style. Here's what I mean https://code.sololearn.com/cR2izd9X6bZ1/?ref=app
20th Jan 2022, 1:38 PM
Sandy Ho
Sandy Ho - avatar
+ 1
Your option is more convenient and better. I have not used the dictionary collection yet, but you showed how it works, thank you very much for your time
21st Jan 2022, 7:27 AM
Роман Жигунов
Роман Жигунов - avatar
0
thanks for the answer Kresnt Mun) For example, I need to enter names with input and they are associated with name[0] then enter 5 names and they are associated with name[1] Console.WriteLine ("enter names") Tom Jon name[0] = Tom, Jon Console.WriteLine ("enter names") Tom1 Jon1 Jon2 name[0] = Tom1, Jon1, Jon2 https://code.sololearn.com/cb5NJTB7XPJN
20th Jan 2022, 9:12 AM
Роман Жигунов
Роман Жигунов - avatar
0
Your code is inputting data indefinitely probably should be changed from if (input == null) on (input != null)
20th Jan 2022, 9:17 AM
Роман Жигунов
Роман Жигунов - avatar