in Education by
Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()); string[] strA = new string[x]; strA[0] = "Hello"; strA[1] = "World"; for(int i = 0;i < x;i++) { Console.WriteLine(strA[i]); } Now, how can I do it with a double array? I've already tried this: Console.WriteLine("Number 1: "); int x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Number 2: "); int y = Convert.ToInt32(Console.ReadLine()); // Got an error, right way string[x][]; // But how can I define the second array? string[][] strA = new string[x][y]; strA[0][0] = "Hello"; strA[0][1] = "World"; strA[1][0] = "Thanks"; strA[1][1] = "Guys"; for(int i = 0;i < x;i++) { for(int j = 0;i < y;i++) { // How can I see the items? Console.WriteLine(strA[i][j]); } } If's there's a simpler way of doing this, i'll be happy to learn it. It's only for knowledge, I'm studying double array for the first time, so have patience please :) Here's my example: https://dotnetfiddle.net/PQblXH JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are working with jagged array (i.e. array of array string[][]), not 2D one (which is string[,]) If you want to hardcode: string[][] strA = new string[][] { // array of array new string[] {"Hello", "World"}, // 1st line new string[] {"Thanks", "Guys"}, // 2nd line }; in case you want to provide x and y: string[][] strA = Enumerable .Range(0, y) // y lines .Select(line => new string[x]) // each line - array of x items .ToArray(); Finally, if we want to initialize strA without Linq but good all for loop (unlike 2d array, jagged array can contain inner arrays of different lengths): // strA is array of size "y" os string arrays (i.e. we have "y" lines) string[][] strA = new string[y][]; // each array within strA for (int i = 0; i < y; ++i) strA[i] = new string[x]; // is an array of size "x" (each line of "x" items) Edit: Let's print out jagged array line after line: Good old for loops for (int i = 0; i < strA.Length; ++i) { Console.WriteLine(); // please, note that each line can have its own length string[] line = strA[i]; for (int j = 0; j < line.Length; ++j) { Console.Write(line[j]); // or strA[i][j] Console.Write(' '); // delimiter, let it be space } } Compact code: Console.Write(string.Join(Environment.newLine, strA .Select(line => string.Join(" ", line))));

Related questions

0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
0 votes
    I want to convert an int to a byte[2] array using BCD. The int in question will come from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I want to make a method that will populate the lst_List list with rows from various tables and fields. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to make a method that will populate the lst_List list with rows from various tables and fields. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to show a confirmation box from c# code rather than JavaScript. Is there any way I can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    import numpy as np from pint import UnitRegistry unit = UnitRegistry() Q_ = unit.Quantity a = 1.0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have an array in C, let's suppose it's **arr = malloc(100 * sizeof(char*)); through ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have an array in C, let's suppose it's **arr = malloc(100 * sizeof(char*)); through ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want put perfect percentage on each chart column as shown in fig in C# Windows Forms. How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I want to put a value from database because my dropdown already have a value the same as the text ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I want put perfect percentage on each chart column as shown in fig in C# Windows Forms. How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I have this XML file and am able to read it, but it breaks and returns null once it gets to the ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
...