【问题标题】:How to add a column to the an array C#如何将一列添加到数组C#
【发布时间】:2016-11-23 09:31:10
【问题描述】:

我可以创建用户输入的动态数组,但我需要有一个用户未输入的起始固定数组:

这是一个例子:

输入并打印的数组为:

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

我需要在数组的开头添加一列 0,如下所示:

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

谢谢!

【问题讨论】:

  • 您可以创建一个带有额外索引的新数组,并在首次插入新的起始列后再次填充它
  • 你想直接打印还是修改实际的矩阵?
  • 修改实际矩阵以在开头有一个额外的列@Reddy

标签: c# arrays loops


【解决方案1】:

您只需要用一个额外的列初始化矩阵并用您需要的值填充它,然后再用用户输入的值填充矩阵的其余部分:

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

【讨论】:

    【解决方案2】:

    假设您为 r 和 c 输入 3 行和 2 列。然后输入数值。输出是:

    1 2

    1 2

    1 2

    但现在我希望它显示为:

    0 1 2

    0 1 2

    0 1 2

    行数(垂直)相同。但是由于我为 0 添加了额外的列,因此列数(水平)从 2 增加到 3。

    所以在代码中我们必须考虑额外的列:

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

    要求用户输入的第一个条目不再是 {0,0},因为第一个条目会自动输入,但您真的在乎吗?如果是这样,您可以从那里的显示值中减去 1:

        Console.Write("Enter value for matrix[{0},{1}] = ", row, col -1);
    

    【讨论】:

    • 工作完美,先生!感谢您并为您提供的额外解释竖起大拇指!
    【解决方案3】:

    您似乎使用了错误的集合类型:二维数组int[,] 而不是List&lt;List&lt;int&gt;&gt;

      ...
      // Initialization is quite a complex, but it's the only such a fragment  
      List<List<int>> matrix = Enumerable
        .Range(1, r)              // r columns
        .Select(i => Enumerable   // i - row index which we ignore
          .Range(1, c)            // c columns 
          .Select(index => 0)     // assign each item to 0 
          .ToList())              // inner list
        .ToList();                // outer list
    
      for (int row = 0; row < r; row++)
      {
         for (int col = 0; col < c; col++)
         {
            Console.Write("Enter value for matrix[{0},{1}] = ", row, col);
            // please, notice [row][col] instead of [row, col]
            matrix[row][col] = (int)Convert.ToInt32(Console.ReadLine());
         }
      }
    

    或者你甚至可以生成初始矩阵

    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    
       List<List<int>> matrix = Enumerable
         .Range(1, 4)               // 4 rows
         .Select(i => Enumerable    // i - row index which we ignore
            .Range(1, 5)            // 5 columns
            .Select(index => index) // assign 1, 2, ..., 5 to row items
            .ToList())              // inner list
         .ToList();                 // outer list  
    

    无需任何用户输入。而当你想添加一列时,就像

      foreach (var row in matrix)
        row.Insert(0, 0);
    

    在每个row0 位置插入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);
    

    【讨论】:

    • 我想要它没有用户输入,没有用户注意到它被插入......我仍然尝试你的代码在选择和插入时出现很多语法错误
    • @Johnny:很抱歉打错字了;应该是.Select(i =&gt; Enumerable // i - row index which we ignore insetad of .Select(Enumerable:即使我们不想要行索引,也必须提到它
    【解决方案4】:

    作为一个泛型方法,简单:

    static TElement[,] AppendColumnOnTheLeft<TElement>(TElement[,] before)
    {
      var after = new TElement[before.GetLength(0), before.GetLength(1) + 1];
      for (var i = 0; i < before.GetLength(0); ++i)
        for (var j = 0; j < before.GetLength(1); ++j)
          after[i, j + 1] = before[i, j];
    
      return after;
    }
    

    新列的条目都将具有值default(TElement),即0nullfalse等。

    【讨论】:

      【解决方案5】:

      Array.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 行以上的行复制到新矩阵中,即可完成工作..

      【讨论】:

        【解决方案6】:

        试试下面的代码

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

        【讨论】:

        • 语法错误 Error 1 无法将类型'int'在线隐式转换为'bool' if (col = 0)
        • 哦,是的,等于现在添加的缺失
        猜你喜欢
        • 1970-01-01
        • 2017-02-22
        • 1970-01-01
        • 2010-10-19
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多