简体   繁体   English

如何制作锯齿阵列?

[英]How to make a Jagged Array?

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 这是我的示例: https : //dotnetfiddle.net/PQblXH

You are working with jagged array (ie array of array string[][] ), not 2D one (which is string[,] ) 您正在使用锯齿状数组 (即数组string[][]的数组),而不是二维 数组 (即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 : 如果您想提供xy

  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 ): 最后,如果我们要在没有Linq的情况下初始化strAfor循环一切有利(与2d数组不同, 锯齿状数组可以包含不同长度的内部数组):

  // 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循环

  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))));

You're using a jagged array instead of a multidimensional one. 您使用的是锯齿状数组,而不是多维 数组 Simply use [,] instead of [][] . 只需使用[,]而不是[][] You can then use it with new string[x, y] . 然后可以将其与new string[x, y]

First you need to clarify thing in your head before writing code. 首先,您需要在编写代码之前弄清楚头脑中的事情。
I will recommend writing in simple phrase what you want to do. 我建议用简单的短语写你想做什么。 The code in your fiddle go into every direction. 小提琴中的代码无处不在。 I will address the code in the fiddle and not your questionas you have typo error and other issue that where not present in the fiddle while the fiddle has error that the question do not have. 我将解决小提琴中的代码,而不是您的问题,因为您有错别字错误和其他问题,而小提琴中没有错误,而小提琴中有该问题没有的错误。

To simplify the problem lets use clear understandable names. 为了简化问题,请使用清晰易懂的名称。 The 2d array will be an table, with rows and columns. 2d数组将是一个具有行和列的表。

//  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];

You need to to declare your table before knowing its size. 您需要先声明表,然后才能知道其大小。

// 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();
    }
}

table.GetLength(0) is equivalent to X, and table.GetLength(1) is equivalent to Y, and can be replace. table.GetLength(0)等效于X, table.GetLength(1)等效于Y,并且可以替换。

// 3/. Display the Table 
Console.Write("\n");
for (int row = 0; row < table.GetLength(0); row++)
{
    for (int col = 0; col < table.GetLength(1); col++)
    {
        Console.Write(table[row, col]);
    }
    Console.Write("\n");
}

For a 3x3 table with 00X, 0X0, X00 as inputs the result is 对于以00X,0X0,X00作为输入的3x3表,结果为

00X
0X0
X00

It works. 有用。 You separate each cell when we display by simply adding a comma or a space. 在显示时,您可以通过简单地添加逗号或空格来分隔每个单元格。 Fiddle 小提琴

And for Jagged Array: 对于锯齿数组:

//  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");
}

Wrong variable in use: 使用了错误的变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM