Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何制作锯齿阵列?_C#_Arrays - Fatal编程技术网

C# 如何制作锯齿阵列?

C# 如何制作锯齿阵列?,c#,arrays,C#,Arrays,像一个简单的数组一样思考: 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]); } Console.WriteLine(“

像一个简单的数组一样思考:

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]);
}
Console.WriteLine(“编号:”);
intx=Convert.ToInt32(Console.ReadLine());
字符串[]strA=新字符串[x];
斯特拉[0]=“你好”;
斯特拉[1]=“世界”;
对于(int i=0;i
现在,我如何使用双数组来实现它

我已经试过了:

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]); 
    }
}
Console.WriteLine(“1号”);
intx=Convert.ToInt32(Console.ReadLine());
控制台。写线(“2号:”);
int y=Convert.ToInt32(Console.ReadLine());
//出现错误,右键字符串[x][];
//但是如何定义第二个数组呢?
字符串[][]strA=新字符串[x][y];
斯特拉[0][0]=“你好”;
strA[0][1]=“世界”;
strA[1][0]=“谢谢”;
斯特拉[1][1]=“伙计们”;
对于(int i=0;i
如果有更简单的方法,我很乐意学习

这只是为了知识,我是第一次学习双阵,请耐心点:)

下面是我的例子:

您使用的是锯齿状数组(即数组的数组<代码>字符串[]【
),而不是二维数组(即<代码>字符串[,]

如果要硬编码:

如果您想提供
x
y

  string[][] strA = Enumerable
    .Range(0, y)                   // y lines
    .Select(line => new string[x]) // each line - array of x items
    .ToArray(); 
最后,如果我们想初始化
strA
而不使用Linq,但对
循环使用良好的所有
(与2d数组不同,锯齿数组可以包含不同长度的内部数组):

紧凑代码:

  Console.Write(string.Join(Environment.newLine, strA
    .Select(line => string.Join(" ", line))));
您使用的是锯齿状数组(即数组的数组<代码>字符串[][]
),而不是二维数组(即<代码>字符串[,]

如果要硬编码:

如果您想提供
x
y

  string[][] strA = Enumerable
    .Range(0, y)                   // y lines
    .Select(line => new string[x]) // each line - array of x items
    .ToArray(); 
最后,如果我们想初始化
strA
而不使用Linq,但对
循环使用良好的所有
(与2d数组不同,锯齿数组可以包含不同长度的内部数组):

紧凑代码:

  Console.Write(string.Join(Environment.newLine, strA
    .Select(line => string.Join(" ", line))));
你用的是a而不是1。只需使用
[,]
而不是
[]]
。然后,您可以将其与新字符串[x,y]
一起使用,因为您使用的是a而不是1。只需使用
[,]
而不是
[]]
。然后,您可以将其与
新字符串[x,y]
一起使用使用中的错误变量:

for(int j = 0;i < y;i++) <- Should be j
{
    // How can I see the items?
    Console.WriteLine(strA[i][j]); 
}
for(int j=0;i
for(int j = 0;i < y;i++) <- Should be j
{
    // How can I see the items?
    Console.WriteLine(strA[i][j]); 
}

for(intj=0;i
我建议你用简单的短语写下你想做的事情。你小提琴里的密码会传到每个方向。我将在提琴中处理代码,而不是你的问题,因为你有打字错误和其他问题,而提琴中没有出现,而提琴有问题没有的错误

为了简化问题,让我们使用清晰易懂的名称。二维数组将是一个表,包含行和列

//  1/. Ask for 2table dim sizes.
Console.WriteLine("Enter number of rows:");
var x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter number of columns:");
var y = int.Parse(Console.ReadLine());

var table = new string[x, y];
在知道表的大小之前,您需要声明表

// 2/. Fill the board
for (int row = 0; row < table.GetLength(0); row++)
{
    for (int col = 0; col < table.GetLength(1); col++)
    {
        Console.WriteLine($"Enter the value of the cell [{row},{col}]");
        table[row, col] = Console.ReadLine();
    }
}
对于输入为00X、0X0、X00的3x3表格,结果为

00X
0X0
X00
它起作用了。在显示时,只需添加逗号或空格,即可将每个单元格分隔开。

对于锯齿阵列:

//  1/. Ask for 2table dim sizes.
Console.WriteLine("Enter number of rows:");
var x = int.Parse(Console.ReadLine());

var table = new string[x][];

// 2/. Fill the board
for (int row = 0; row < table.GetLength(0); row++)
{
    Console.WriteLine($"Enter of the line n°{row}");
    var lineSize = int.Parse(Console.ReadLine());
    table[row] = new string[lineSize];
    for (int col = 0; col < table[row].Length; col++)
    {
        Console.WriteLine($"Enter the value of the cell [{row},{col}]");
        table[row][col] = Console.ReadLine();
    }
    Console.Write("\n");
}

// 3/. Display the Table 
Console.Write("\n");
for (int row = 0; row < table.Length; row++)
{
    for (int col = 0; col < table[row].Length; col++)
    {
        Console.Write(table[row][col]);
    }
    Console.Write("\n");
}
//1/。要求提供2个表暗尺寸。
Console.WriteLine(“输入行数:”);
var x=int.Parse(Console.ReadLine());
变量表=新字符串[x][];
// 2/. 填补空缺
for(int row=0;row
在编写代码之前,首先需要在头脑中澄清一些事情。
我建议你用简单的短语写下你想做的事情。你小提琴里的密码会传到每个方向。我将在提琴中处理代码,而不是你的问题,因为你有打字错误和其他问题,而提琴中没有出现,而提琴有问题没有的错误

为了简化问题,让我们使用清晰易懂的名称。二维数组将是一个表,包含行和列

//  1/. Ask for 2table dim sizes.
Console.WriteLine("Enter number of rows:");
var x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter number of columns:");
var y = int.Parse(Console.ReadLine());

var table = new string[x, y];
在知道表的大小之前,您需要声明表

// 2/. Fill the board
for (int row = 0; row < table.GetLength(0); row++)
{
    for (int col = 0; col < table.GetLength(1); col++)
    {
        Console.WriteLine($"Enter the value of the cell [{row},{col}]");
        table[row, col] = Console.ReadLine();
    }
}
对于输入为00X、0X0、X00的3x3表格,结果为

00X
0X0
X00
它起作用了。在显示时,只需添加逗号或空格,即可将每个单元格分隔开。

对于锯齿阵列:

//  1/. Ask for 2table dim sizes.
Console.WriteLine("Enter number of rows:");
var x = int.Parse(Console.ReadLine());

var table = new string[x][];

// 2/. Fill the board
for (int row = 0; row < table.GetLength(0); row++)
{
    Console.WriteLine($"Enter of the line n°{row}");
    var lineSize = int.Parse(Console.ReadLine());
    table[row] = new string[lineSize];
    for (int col = 0; col < table[row].Length; col++)
    {
        Console.WriteLine($"Enter the value of the cell [{row},{col}]");
        table[row][col] = Console.ReadLine();
    }
    Console.Write("\n");
}

// 3/. Display the Table 
Console.Write("\n");
for (int row = 0; row < table.Length; row++)
{
    for (int col = 0; col < table[row].Length; col++)
    {
        Console.Write(table[row][col]);
    }
    Console.Write("\n");
}
//1/。要求提供2个表暗尺寸。
Console.WriteLine(“输入行数:”);
var x=int.Parse(Console.ReadLine());
变量表=新字符串[x][];
// 2/. 填补空缺
for(int row=0;row
你写的东西怎么了?双数组?像是双精度数组还是二维数组?你们有点误解,这些不是双数组,它们被称为二维数组(有一个,两个,三个…n维数组)。双数组将是doubles@MindSwipe它不是一个二维数组,它是一个锯齿状数组。二维数组应该是
string[,]
@LeoHen