C#数组数组 - 帮助 [英] C# arrays of array - help

查看:101
本文介绍了C#数组数组 - 帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,所以我刚开始学习C#,我是编程的新手,对任何编程语言都没有过去的知识。我正在从RB Whitaker的书中学习C# - C#Players Guide。

我已经谈到了关于数组的章节,我不太明白这个代码,它说值得简要介绍一下如何在这些更复杂的数组中查看每个元素。对于数组数组,或者是锯齿状数组,这可能如下所示:

  int  [] [] matrix =  new   int  [ 4 ] []; 
矩阵[ 0 ] = new int [ 2 ];
矩阵[ 1 ] = new int [ 6 < /跨度>];
// 继续填写锯齿状数组的值......
< span class =code-keyword> for
int row = 0 ; row < span class =code-keyword>< matrix.Length; row ++)
{
for int column = 0 ;列< matrix [row] .Length;专栏++)
{
Console.Write(matrix [row] [column] + ); // 行中的每个项目用空格分隔
}
Console.WriteLine (); // 行数分隔行
}



这就是代码,希望有人可以简单解释一下。谢谢。



我尝试了什么:



据我所知,在第一个for循环中,变量行被赋值并且0被分配给它,但是matrix.Length应该在这些数组中是什么? ?它是否计算该数组中的4个数组或这些数组中的每个元素,这让我感到困惑......我的大脑阻塞了这一点,我不理解任何代码之前的代码,所以我很感激有人可以解释这个简单地给我写一下代码,谢谢你。

解决方案

这是一个锯齿状的数组,是一个包含不同大小数组的数组。如果您满足作者的评论请求,那么您可能很容易看到发生了什么:

  int  [] [] matrix =  new   int  [ 4  ] []; 
矩阵[ 0 ] = new int [ 2 ];
矩阵[ 1 ] = new int [ 6 ];
// 锯齿状数组的值
// 步骤1,完成'数组数组'
矩阵[ 2 ] = new int [ 1 ];
矩阵[ 3 ] = new int [ 3 ];
// 第2步,为所有数组的所有项目赋值。
矩阵[ 0 ] [ 0 ] = 1 < /跨度>;矩阵[ 0 ] [ 1 ] = 5 ;
矩阵[ 1 ] [ 1 ] = 4 ;矩阵[ 1 ] [ 1 ] = 2 ;矩阵[ 1 ] [ 2 ] = 4 ;矩阵[ 1 ] [ 3 ] = 2 ;矩阵[ 1 ] [ 4 ] = 8 ;矩阵[ 1 ] [ 5 ] = 4 ;
矩阵[ 2 ] [ 0 ] = 7 ;
矩阵[ 3 ] [ 0 ] = 9 ;矩阵[ 3 ] [ 1 ] = 4 ;矩阵[ 3 ] [ 2 ] = 7 ;

for int row = 0 ; row < matrix.Length; row ++)
{
for int column = 0 ;列< ; matrix [row] .Length; column ++)
{
Console.Write(matrix [row] [column] + ); // 行中的每个项目用空格分隔
}
Console.WriteLine (); // 行数分隔行
}


Hello everyone so I just started learning C#,I am newbie to programming and have no past knowledge of any programming languages.I am learning C# from the book of RB Whitaker - The C# Players Guide.
And I have come to the chapter where it talks about arrays,I dont quite understand this code,it says "It is worth briefly describing how you might go about looking at each element in these more complicated arrays. For an array of arrays, or a jagged array, this might look like this:

int[][] matrix = new int[4][];
matrix[0] = new int[2];
matrix[1] = new int[6];
// Continue filling in values for the jagged array...
for(int row = 0; row < matrix.Length; row++)
{
for(int column = 0; column < matrix[row].Length; column++)
{
Console.Write(matrix[row][column] + " "); // Each item in the row separated by spaces
}
Console.WriteLine(); // Rows separated by lines
}


That's the code,hope someone can explain it briefly.Thank You.

What I have tried:

As far as I understand in the first for loop,variable row is made and 0 is assigned to it,but what is matrix.Length supposed to be here in these array of arrays?? Does it count 4 arrays inside that array or every single element inside those arrays,that's confusing me...My brain blocks at this point and I dont understand any code ahead of that one,so I'd be grateful is someone can explain this code to me briefly,Thank You again.

解决方案

It is a a jagged array, that is an array containing arrays of different sizes. If you fulfill the author's remark request then you might easily see what is going on:

int[][] matrix = new int[4][];
  matrix[0] = new int[2];
  matrix[1] = new int[6];
  // values for the jagged array
  // step 1, complete the 'array of arrays'
  matrix[2] = new int[1];
  matrix[3] = new int[3];
  // step 2, assign values to all the items of all the arrays.
  matrix[0][0] = 1; matrix[0][1] = 5;
  matrix[1][1] = 4; matrix[1][1] = 2; matrix[1][2] = 4; matrix[1][3] = 2; matrix[1][4] = 8; matrix[1][5] = 4;
  matrix[2][0] = 7;
  matrix[3][0] = 9; matrix[3][1] = 4; matrix[3][2] = 7;

  for (int row = 0; row < matrix.Length; row++)
  {
    for (int column = 0; column < matrix[row].Length; column++)
    {
      Console.Write(matrix[row][column] + " "); // Each item in the row separated by spaces
    }
    Console.WriteLine(); // Rows separated by lines
  }


这篇关于C#数组数组 - 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆