C# 如何在数组C中添加列#
我可以创建用户输入的动态数组,但我需要一个用户未输入的起始固定数组:C# 如何在数组C中添加列#,c#,arrays,loops,C#,Arrays,Loops,我可以创建用户输入的动态数组,但我需要一个用户未输入的起始固定数组: //... int[,] matrix = new int[r, c + 1]; /*Insert Values into Main Matrix --------------------------------------------------------------------------------*/ for (int row = 0; row < r; row++)
//...
int[,] matrix = new int[r, c + 1];
/*Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int row = 0; row < r; row++)
{
//Fill the first column manually
matrix[row, 0] = 0;
//This loop then starts from the second column, and loops until
//col <= c instead of just col < c
for (int col = 1; col <= c; col++)
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
//...
这里有一个例子:
输入并打印的数组为:
123445
123445
123445
123445
我需要在数组的开头添加一列0,如下所示:
01 2 3 4 5
01 2 3 4 5
01 2 3 4 5
01 2 3 4 5
谢谢大家! 在用用户输入的值填充矩阵的其余部分之前,只需使用一个额外的列初始化矩阵并用所需的值填充它:
//...
int[,] matrix = new int[r, c + 1];
/*Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int row = 0; row < r; row++)
{
//Fill the first column manually
matrix[row, 0] = 0;
//This loop then starts from the second column, and loops until
//col <= c instead of just col < c
for (int col = 1; col <= c; col++)
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
//...
/。。。
int[,]矩阵=新的int[r,c+1];
/*将值插入主矩阵
--------------------------------------------------------------------------------*/
for(int行=0;行 //col在用用户输入的值填充矩阵的其余部分之前,只需使用一个额外的列初始化矩阵,并用所需的值填充它:
//...
int[,] matrix = new int[r, c + 1];
/*Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int row = 0; row < r; row++)
{
//Fill the first column manually
matrix[row, 0] = 0;
//This loop then starts from the second column, and loops until
//col <= c instead of just col < c
for (int col = 1; col <= c; col++)
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
//...
/。。。
int[,]矩阵=新的int[r,c+1];
/*将值插入主矩阵
--------------------------------------------------------------------------------*/
for(int行=0;行
int r;
int c;
Console.Write("Enter number of rows: ");
r = (int)Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number of columns: ");
c = ((int)Convert.ToInt32(Console.ReadLine()) + 1);
int[,] matrix = new int[r, c];
/*Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
if(col == 0)
{
matrix[row, col] = 0;
}
else{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col-1);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
}
intr;
INTC;
Console.Write(“输入行数:”);
r=(int)Convert.ToInt32(Console.ReadLine());
Console.Write(“输入列数:”);
c=((int)Convert.ToInt32(Console.ReadLine())+1);
int[,]矩阵=新的int[r,c];
/*将值插入主矩阵
--------------------------------------------------------------------------------*/
for(int行=0;行
尝试下面的代码
int r;
int c;
Console.Write("Enter number of rows: ");
r = (int)Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number of columns: ");
c = ((int)Convert.ToInt32(Console.ReadLine()) + 1);
int[,] matrix = new int[r, c];
/*Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
if(col == 0)
{
matrix[row, col] = 0;
}
else{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col-1);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
}
intr;
INTC;
Console.Write(“输入行数:”);
r=(int)Convert.ToInt32(Console.ReadLine());
Console.Write(“输入列数:”);
c=((int)Convert.ToInt32(Console.ReadLine())+1);
int[,]矩阵=新的int[r,c];
/*将值插入主矩阵
--------------------------------------------------------------------------------*/
for(int行=0;行
似乎您使用了错误的集合类型:2D arrayint[,]
而不是List
:
在每个行的0
第th个位置插入0
。测试
// join the matrix while separating rows with new lines and items with spaces
var report = string.Join(Environment.NewLine, matrix
.Select(row => string.Join(" ", row)));
Console.Write(report);
似乎您使用了错误的集合类型:2D arrayint[,]
而不是List
:
在每个行的0
第th个位置插入0
。测试
// join the matrix while separating rows with new lines and items with spaces
var report = string.Join(Environment.NewLine, matrix
.Select(row => string.Join(" ", row)));
Console.Write(report);
Copy将允许您复制一个数组并将其移到上面,例如
int[,] arr = { 1, 2, 3, 4, 5 } ;
int[,] newarr = new int[6];
Array.Copy(arr, 0, newarr, 1, arr.Length);
为了打字等目的,我缩短了你的数组,但这会导致一个数组{0,1,2,3,4,5}您不能只将其应用于多行,因此您需要将行写入,我认为它有效,但我检查了,结果无效。因此,您只需为矩阵中的每一行,将1上的行复制到新矩阵中,工作完成。数组。copy允许您复制数组并将其移动,例如
int[,] arr = { 1, 2, 3, 4, 5 } ;
int[,] newarr = new int[6];
Array.Copy(arr, 0, newarr, 1, arr.Length);
为了打字等目的,我缩短了你的数组,但这会导致一个数组{0,1,2,3,4,5}你不能只将它应用于多行,所以你需要对你的行进行写入,我认为它有效,但我检查了,结果无效。因此,你只需对矩阵中的每一行,将1以上的行复制到新矩阵中,工作完成。假设你为r和c输入3行2列。然后输入值。输出为:
1 2
1 2
1 2
但现在我希望它显示为:
0112
0112
0112
行数(垂直)相同。但是列数(水平)从2增加到了3,因为我为0添加了额外的列
因此,在代码中,我们必须考虑额外的列:
int r;
int c;
Console.Write("Enter number of rows: ");
r = (int)Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number of columns: ");
c = (int)Convert.ToInt32(Console.ReadLine());
c++; //add an extra column for the added 0's
int[,] matrix = new int[r, c];
现在,我们希望将0添加到此列。但我们需要能够指定,我们只希望为第一列添加0。填充数组时,我们可以添加一个检查,说明如果当前填充的列是行的第一列,则会自动插入0
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
if (col == 0)
{
matrix[row, col] = 0;
}
else
{
Console.Write("Enter value for matrix[{0},{1}] = ", row, col);
matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
}
}
}
假设为r和c输入3行2列。然后输入值。输出为:
1 2
1 2
1 2
但现在我希望它显示为:
0112
0112
0112
行数(垂直)相同。但是列数(水平)从2增加到了3,因为我为0添加了额外的列
因此,在代码中,我们必须考虑额外的列:
int r;
int c;
Console.Write("Enter number of rows: ");
r = (int)Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number of columns: ");
c = (int)Convert.ToInt32(Console.ReadLine());
c++; //add an extra column for the added 0's
int[,] matrix = new int[r, c];
现在我们想将0添加到此列。但我们需要