簡體   English   中英

如何將列添加到數組C#

[英]How to add a column to the an array C#

我能夠創建一個使用時輸入的動態數組,但是我需要有一個用戶未輸入的起始固定數組:

這是一個例子:

輸入並打印的數組是:

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

謝謝!

您只需要用額外的列初始化矩陣,然后用您需要的值填充矩陣,然后再用用戶輸入的值填充矩陣的其余部分:

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

假設您為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);

看來,您使用了錯誤的集合類型 :2D數組int[,]而不是List<List<int>>

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

作為通用方法,簡單:

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等。

試試下面的代碼

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

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以上的行復制到新矩陣中,就可以完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM