【问题标题】:How can we find items count in the C# integer array?我们如何在 C# 整数数组中找到项目数?
【发布时间】:2018-08-09 00:35:52
【问题描述】:

我需要在 C# 数组中找到整数类型的项目计数。

我的意思是;

int[] intArray=new int[10]
int[0]=34
int[1]=65
int[2]=98

intArray 的项目数为 3。

我在下面找到了 strArray 的代码,但它不适用于 int 数组。

string[] strArray = new string[50];
...
int result = strArray.Count(s => s != null);

【问题讨论】:

  • 您使用数组而不是列表是否有原因

标签: c# arrays count integer


【解决方案1】:

嗯,首先你必须决定什么是无效值。是0吗?如果是这样,您可以这样做:

int result = intArray.Count(i => i != 0);

请注意,这只是因为默认情况下,int 数组的元素被初始化为零。如果 0 在您的情况下最终有效,您必须事先使用不同的无效值填充数组。

另一种方法是使用可为空的类型:

int?[] intArray = new int?[10];
intArray[0] = 34;
intArray[1] = 65;
intArray[2] = 98;

int result = intArray.Count(i => i.HasValue);

【讨论】:

  • 唯一的问题是这会告诉你有多少有价值,而不是顺序。如果 intArray[1] 为 null 但 intArray[3] 不是,则不能假设前 3 个项目有值,但计数将返回 3
  • 确实如此,但 OP 从未表示所有值都必须按顺序排列。 OP 只是将其称为“项目计数”,而他们的示例可能恰好是按顺序排列的。
  • 这很有用,但如果零是数组中的有效值,则会出现问题...
  • @ReedCopsey 正如我在回答的第一行中指出的那样。 :)
  • @itsme86 是的 - 但如果零不是无效值,则此技术根本不起作用。您的行表明任何无效值(即:-999)都可以工作,除非您提前明确地将数组初始化为无效值,否则这是不正确的。零恰好起作用,只是因为数组自动用零值初始化,但如果零不是无效的,那么它就会崩溃。 (我的回答提供了另一种适用于这种情况的替代方案......虽然我确实投票支持你,因为它是合理的,并且直接回答了所提出的问题。)
【解决方案2】:

虽然 itsme86 为您提供了 good answer to your actual question,但我怀疑您最好重新考虑如何完全编写此内容。

如果这是您的目标,我建议您换个角度思考。您可能需要考虑使用List<int>,而不是分配固定大小的数组,并且只为其分配特定的值:

List<int> intList = new List<int>();

intList.Add(34);
intList.Add(65);
intList.Add(98);

项目的数量将始终为intList.Count,您可以通过这种方式添加任意数量的项目,而不必担心“分配的大小”,因为列表会根据需要自动增长。如果您将0 添加到列表中作为一个实际值,它也不会给您带来不好的结果,如果它是一个有效值,计算非零元素将不计为零。

请注意,您也可以按索引访问项目,就像使用数组一样:

int secondValue = intList[1]; // Access like you do with arrays

【讨论】:

  • 3 年后,我发现这个答案是正确的,因为所选答案不能解决 0 是其中一个元素的可能(和预期!)值的情况!
【解决方案3】:
int[] intArray=new int[3]  // Edit: Changed this to 3 to make my answer work. :)
int[0]=34
int[1]=65
int[2]=98

int count = intArray.Length; // <-- Is this what you're after?

编辑:

咳咳。正如我谦虚地指出的那样,Length 将返回数组中的元素总数,在您的示例中为 10。如果您正在寻找 非零 的数量数组中的元素,您应该按照其他一些答案中的建议进行操作。

【讨论】:

  • @bonesbrigade 我做了吗?我没有看到我对原始问题的任何编辑?你在说什么?
  • 第 1 行:// Edit: Changed this to 3 to make my answer work. :)
  • @bonesbrigade:天哪。您显然想要传达的是我修改了我的答案,而不是问题。现在这到底有什么问题?
【解决方案4】:

当您初始化整数数组而不指定任何值时,C# assigns a value of zero to every element。因此,如果零不是您的数组的有效值,您可以随时进行测试。

或者,您可以将数组的元素初始化为在您的上下文中无效的某个值(即,如果负数无效,则初始化为 -1),然后循环遍历数组,计算有效元素。

【讨论】:

    【解决方案5】:

    如果保证数组只能按顺序访问,您可以通过一些分而治之的方式击败完整的迭代 IEnumerable Count(对于较大的数组),例如

    static int DivideCount(int[] arr, int idx, int bottom, int top)
    {
        if (idx <= 0)
            return 0;
        else if (idx >= arr.Length - 1)
            return arr.Length;
        else if (arr[idx] == 0 && arr[idx - 1] != 0)
            return idx;
        else if (arr[idx] == 0 && arr[idx - 1] == 0)
            return DivideCount(arr, bottom + ((idx - bottom) / 2), bottom, idx);
        else if (arr[idx] != 0 && arr[idx - 1] != 0)
            return DivideCount(arr, top - ((top - idx) / 2), idx, top);
        else
            return -1;  // hello compiler
    }
    
    
    
    int[] intArray = new int[10];
    intArray[0] = 35;
    intArray[1] = 65;
    intArray[2] = 98;
    
    var count = DivideCount(intArray, intArray.Length / 2, 0, intArray.Length);
    

    【讨论】:

      【解决方案6】:

      如果除您之外的其他人初始化了数组(即您无法将数组值初始化为无效值 -- null、-1 等),则上述解决方案都不是最佳的。

      假设你有一个数组:

      var arr = new[] {0, 10, 18, 0, 20, 0, 0, 0, 0, 0, 0, 0};
      

      如果你只计算零条目的数量:

      int result = arr.Count(i => i != 0);
      

      Count() 返回 3,而实际上已经初始化了 5 个条目。例如,从音频文件中读取到缓冲区的原始字节数组,您想知道最后读取的元素的索引。

      一种不完美但可以满足您要求的替代方法是查找最后一个非零条目,如下所述:Linq - Get the Index of the Last Non-Zero Number of Array

      【讨论】:

        猜你喜欢
        • 2016-01-10
        • 1970-01-01
        • 2020-02-27
        • 1970-01-01
        • 2022-11-14
        • 2016-10-30
        • 2019-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多