【问题标题】:Adding elements from a list to a 2d array in c# [closed]在c#中将元素从列表添加到二维数组[关闭]
【发布时间】:2021-02-15 17:11:40
【问题描述】:

我有一个名为 Matrix 的类,所以基本上我创建了一个名为 addElemts 的方法,该方法将整数值列表作为参数。 现在我想获取列表中的值并将它们放入矩阵中,但我使用的逻辑似乎是获取列表中的最后一个元素,这不是预期的结果。

        public void addElemts(List<int> lst)
        {
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                {
                    foreach (int itm in lst)
                    {
                        a[i, j] = itm;
                    }
                }
        }

【问题讨论】:

  • 首先,你需要重新考虑这一行foreach (int itm in lst)
  • 你考虑过 Math.Net 吗:nuget.org/packages/MathNet.Numerics
  • @MitchWheat lemme 通读
  • 使用您的代码,对于每个矩阵项,您将分配列表中的每个元素,每个元素都会覆盖前一个元素,因此您始终可以在任何地方获得最后一个元素。列表应该如何放置在矩阵中?

标签: c# list loops multidimensional-array foreach


【解决方案1】:

忽略任何其他概念或其他问题

有两种简单的方法可以实现这一点,数学,或BlockCopy

从源数组中复制指定数量的字节 从特定位置开始到目标数组的特定偏移量 偏移量。

给定

private static int[,] _matrix = new int[3, 3];

示例 1

public static void Add(List<int> lst)
{
   var height = _matrix.GetLength(0);
   var width = _matrix.GetLength(1);

   if (lst.Count != width * height)
      throw new ArgumentException($"The number of elements in lst ({lst.Count}) is not equal to the matrix {width * height}");
   for (var y = 0; y < height; y++)
   for (var x = 0; x < width; x++)
      _matrix[y, x] = lst[x + width * y];
}

以下所有示例都有效,因为为 2d 数组分配的后备内存具有传染性

示例 2

public static void Add2(List<int> lst)
{
   var width = _matrix.GetLength(0);
   var height = _matrix.GetLength(1);

   if (lst.Count != width * height)
      throw new ArgumentException($"The number of elements in lst ({lst.Count}) is not equal to the matrix {width * height}");

   Buffer.BlockCopy(lst.ToArray(), 0, _matrix, 0, lst.Count * sizeof(int));
}

用法

Add(new List<int>{1,2,3,4,5,6,7,8,9});

使用以下方法,它们没有任何优势(速度或其他)...

奖励方式1,指针

public static unsafe void Add3(List<int> lst)
{
   var width = _matrix.GetLength(0);
   var height = _matrix.GetLength(1);

   if (lst.Count != width * height)
      throw new ArgumentException($"The number of elements in lst ({lst.Count}) is not equal to the matrix {width * height}");

   fixed (int* p = _matrix)
      for (var i = 0; i < lst.Count; i++)
         p[i] = lst[i];
}

奖励方式 2,PInvoke memcpy

[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, uint count);

public static unsafe void Add4(List<int> lst)
{
   var width = _matrix.GetLength(0);
   var height = _matrix.GetLength(1);

   if (lst.Count != width * height)
      throw new ArgumentException($"The number of elements in lst ({lst.Count}) is not equal to the matrix {width * height}");

   fixed (int* pDest = _matrix, pSource = lst.ToArray())
      memcpy((IntPtr)pDest, (IntPtr)pSource, (uint)(lst.Count * sizeof(int)));
}

【讨论】:

  • 矩阵必须是方阵吗?
  • @EmmanuelManoloSiame 不,不是。它适用于任何二维维度,抱歉修复了第一个示例中的错字
  • 如果我尝试一个不是方阵的矩阵,我会得到“索引超出范围”
  • @EmmanuelManoloSiame dotnetfiddle.net/2L1DIO
  • 谢谢,效果很好,我想让用户确定尺寸,我已经成功了,再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 2012-08-19
  • 1970-01-01
  • 2019-04-22
  • 2021-01-26
相关资源
最近更新 更多