【问题标题】:How would you create these arrays? [closed]你将如何创建这些数组? [关闭]
【发布时间】:2021-03-26 20:56:10
【问题描述】:

我有一个“项目”,我只能使用数组。在这个项目中,我必须从 txt 文件中读取数据,然后例如在跑步者中找到第二好的单圈时间等。

我总能计算出正确的答案,但我不确定我的阵列管理。

如您所知,在 C# 中,您必须在初始化时提供数组的长度。由于有些任务我不知道数组的大小,我所做的是声明一个更大的数组,然后使用这个函数:

private static string[] ArraySimplyfier(string[] strs, int index)
{
    string[] returnStr = new string[index];

    for (int i = 0; i < returnStr.Length; i++)
    {
        returnStr[i] = strs[i];
    }

    return returnStr;
}

如您所见,它“清除”了数组中的空值,但问题是我必须为每种类型的数组创建它,这并不理想。您对我的问题有什么其他想法吗?

【问题讨论】:

  • 我很确定您可以在初始化第一个数组之前计算所需的长度。如果它是随机的(如用户输入),那么您不应该使用数组。向我们展示最初的问题,以便我们提供帮助。
  • 你可以使用 IEnumerable 吗?
  • 我认为这就是这个项目的目的:向您展示数组是固定大小的。如果您想动态增长,请改用List&lt;T&gt;
  • "I have to create it for each type of array which is not ideal." 是什么意思?您的代码有效,如果您必须做一个展示数组如何工作的练习,那么您已经完成了。
  • 所以这是我项目中的一个例子:数据库中有很多汽车,例如,我必须将所有红色的汽车收集到一个数组中。在收集所有元素之前,我不会真正知道数组有多大。是的,它有效,我只是好奇也许有人知道更好的方法

标签: c# arrays .net


【解决方案1】:

您可以使用泛型。这样你就可以为每种数组类型提供一种方法。

你的方法应该是这样的:

private static T[] ArraySimplyfier<T>(T[] array, int index)
        {
            T[] result = new T[index];

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = array[i];
            }

            return result;
        }

你可以这样调用这个方法:

//example for a string array
ArraySimplyfier<string>(new string[45], 17); //replace 'new string[45]' with your array

【讨论】:

  • 泛型如何解决未知数组大小的问题?
  • 它允许我为任何数组调用 ArraySimplifier
  • 他说“你可以看到它“清除”数组中的空值,但问题是我必须为每种类型的数组创建它,这并不理想。你还有其他想法吗为了我的问题?”所以对我来说,他的解决方案听起来很有效,但他需要为每种可能的数据类型编写相同的方法。使用泛型就不再需要了。
  • 确实,您似乎是唯一关注问题的那一点的人。
  • 是的,提供的所有方法都很好并且有效,但我会选择@Tomsen 的解决方案!
【解决方案2】:

创建新的、更大的数组和复制是要走的路。你在正确的轨道上。这基本上就是 List 在幕后所做的事情(以优化的方式)。

如果您需要为多种类型提供此功能,您可以查看泛型方法。泛型允许您为多种类型指定类和方法。

【讨论】:

    【解决方案3】:

    所以您的问题似乎是“在过滤数组时,我如何事先知道元素的数量,以获取目标数组的大小?” - 你不知道。

    要么在源数组上循环两次,一次获取计数然后进行复制,要么从一个同样大小的数组开始,保持计数和truncate the array by copying it to a new one with the size capped to the number of results

    【讨论】:

      【解决方案4】:

      List 变得太小时,它的大小会加倍,你可以这样做。检查 github 上 List 的“Add/EnsureCapacity”实现。

      // Adds the given object to the end of this list. The size of the list is
      // increased by one. If required, the capacity of the list is doubled
      // before adding the new element.
      //
      public void Add(T item) {
          if (_size == _items.Length) EnsureCapacity(_size + 1);
          _items[_size++] = item;
          _version++;
      }
      
      // Ensures that the capacity of this list is at least the given minimum
      // value. If the currect capacity of the list is less than min, the
      // capacity is increased to twice the current capacity or to min,
      // whichever is larger.
      private void EnsureCapacity(int min) {
          if (_items.Length < min) {
              int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
              // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
              // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
              if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
              if (newCapacity < min) newCapacity = min;
              Capacity = newCapacity;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-01
        • 2019-09-27
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-17
        • 1970-01-01
        相关资源
        最近更新 更多