【问题标题】:How to access and re-format Jagged Array in C#如何在 C# 中访问和重新格式化锯齿状数组
【发布时间】:2014-12-17 18:17:37
【问题描述】:

我在 c# 中有二维数组,如下所示:

int[][] 2darray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

我怎样才能得到一列作为普通数组,比如

int[] array = 2darray[1][]; //example, not working

并且拥有

int[] array = {3,4};

? 谢谢。

【问题讨论】:

  • int[][] 是锯齿状数组,而不是二维数组。
  • 您期望“列”返回什么 - 现有答案将为您提供锯齿状数组中的第 N 个数组,例如{1, 2},如果按列表示每个子数组的第 N 个元素,例如{1, 3, 5, 7}你需要做更多的工作。
  • 这里是要列还是行? {3,4} 似乎是第二行,而在我看来,例如第一个 column 应该是 {1,3,5,7}
  • 这正是我要找的,谢谢。

标签: c# arrays multidimensional-array jagged-arrays


【解决方案1】:

您的代码无法编译的原因有多种

这样就可以了:

int[][] array2d = { new[]{ 1, 2 }, new[]{ 3, 4 }, new[]{ 5, 6 }, new[]{ 7, 8 } };

int[] array = array2d[0];

问题:

  • 2darray 不是有效的变量名
  • 索引错误
  • 原数组初始化错误


编辑: 正如@heltonbiker 所说,如果您需要第一列的所有元素,您可以使用:

int[] col = array2d.Select(row => row[0]).ToArray();

【讨论】:

  • 看起来 OP 弄错了,他想要一个专栏,请参阅原始问题中的 cmets。
  • int[] 数组 = array2d[0];这正是我想要的。谢谢。 (array2d 是数组,哪个函数作为参数 | 2darray 可能不是有效名称,我将其作为示例编写。即使是其余的,我认为它如何工作。)
【解决方案2】:

对于两列四行的数组,可以这样使用LINQ:

using System.Linq;

first_column = _2darray.Select(row => row[0]).ToArray();

请注意,更改第一个或第二个数组不会更改另一个。

【讨论】:

  • 我并不是说它是唯一的,甚至不是最好的解决方案。但这肯定是一个解决方案。
  • (并且 OP 从未说过他需要修改原始数组)
  • 没有捕捉到列部分。我的错。在您的答案中添加了一条注释,以澄清我以前的担忧。如果你不喜欢它,请返回。 +1
【解决方案3】:

您在 C# 中混淆了 jagged arraysmultidimensional arrays。虽然它们相似,但存在细微差别。锯齿状数组中的行可以具有不同数量的元素,而在二维数组中它们具有相同的长度。因此,在使用锯齿状数组时,您需要记住为丢失的列元素编写处理。我在下面编写了一个示例控制台应用程序来展示它们是如何工作的——它使用 0 作为缺失元素的替代品,但你可以抛出错误等:

using System.Collections.Generic;

namespace JaggedArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //jagged array declaration
            int[][] array1;

            //jagged array declaration and assignment
            var array2 = new int[][] {
                          new int[] { 1, 2 },
                          new int[] { 3, 4 },
                          new int[] { 5, 6 },
                          new int[] { 7, 8 }
                        };

            //2D-array declaration
            int[,] array3;

            //2D-array declaration and assignment (implicit bounds)
            var array4 = new int[,] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};

            //2D-array declaration and assignment (explicit bounds)
            var array5 = new int[4, 2] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};

            //get rows and columns at index
            var r = GetRow(array2, 1); //second row {3,4}
            var c = GetColumn(array2, 1); //second column {2,4,6,8}
        }

        private static int[] GetRow(int[][] array, int index)
        {
            return array[index]; //retrieving the row is simple
        }

        private static int[] GetColumn(int[][] array, int index)
        {
            //but things get more interesting with columns
            //especially if jagged arrays are involved
            var retValue = new List<int>();
            foreach (int[] r in array)
            {
                int ub = r.GetUpperBound(0);
                if (ub >= index) //index within bounds
                {
                    retValue.Add(r[index]);
                }
                else //index outside of bounds
                {
                    retValue.Add(0); //default value?
                    //or you can throw an error
                }
            }
            return retValue.ToArray();
        }
    }
}

【讨论】:

    【解决方案4】:

    试试这个,应该可以的

    int[] array = array2d[1];
    

    将变量名改为array2d,变量不能以数字开头,变量可以字母或下划线开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2011-08-21
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多