Подача 2D массива от пользователя - PullRequest
0 голосов
/ 21 сентября 2018
            Console.WriteLine("\n\t\t\tTwo Dimensional Array");
            Console.Write("\nEnter the number of rows in the array : ");
            int row = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter the number of columns in the array : ");
            int column = Convert.ToInt32(Console.ReadLine());

            string[,] array1;
            array1 = new string[row,column];

            for(int i = 0; i < array1.Length; i++)
            {
                Console.Write("\nEnter the {0}th Row Element : ", i);
                array1[i, 0] = Console.ReadLine();

                for (int j = 0; j < array1.Length; j++)
                {
                    Console.Write("\t& {0}th Column Element : ", j);
                    array1[0,j] = Console.ReadLine();
                }
            }

            for (int i = 0; i < array1.Length; i++)
            {
                Console.Write("\nArray Element at {0}th Row is : {1}", i, array1[i,0]);
                for (int j = 0; j < array1.Length; j++)
                {
                    Console.Write("\t& the {0}th Column is : {1}",j,array1[0,j]);
                }
            }

            Console.ReadLine();

Я хочу принять от пользователя следующее в консоли: 1) Ранги (количество строк и столбцов) 2) Данные для массива

Я создал выше, однако яполучить ошибку исключения, состоящую из «Индекс выходит за пределы диапазона»

Какие ошибки допущены?

1 Ответ

0 голосов
/ 21 сентября 2018

Вам нужна длина каждого измерения в массиве с помощью GetLength () .

Изменение:

for(int i = 0; i < array1.Length; i++)

Кому:

for(int i = 0; i < array1.GetLength(0); i++)

и изменить:

for(int j = 0; j < array1.Length; j++)

К:

for(int j = 0; j < array1.GetLength(1); j++)
...