c# - 从用户处归档二维数组
问题描述
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)数组的数据
我已经创建了上述内容,但是,出现异常错误,包括“索引超出范围”
犯了哪些错误?
解决方案
您需要使用 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++)
推荐阅读
- free-jqgrid - 无法在我的 free-jqgrid 实现中使用我自己的图标图像
- spring - JPA单向映射以插入子实体列表以及父实体+分别插入子实体
- c - 如何输入一个整数并确定是否有两个相同的数字并排?(C)
- jekyll - Github 页面布局
- javascript - 如何使用javascript从datalist中的特殊属性获取数据?
- go - 如果我不指定 GOPATH,去哪里下载我的依赖项?
- bash - 需要在 groovy Jenkins 脚本中运行 Bash 命令
- c++ - C++ 中的浮点数(float 和 double)
- python - Keras 中 RNN/LSTM 的简单时间序列问题
- html - 手机版SHOPIFY图片重叠成文字