【问题标题】:Incrementing the values in an array递增数组中的值
【发布时间】:2013-10-30 11:36:07
【问题描述】:

我正在尝试将一组值分成直方图的箱。我的直方图中将有 10 个 bin。为了对每个箱子中的案例数量进行排序和计算,我使用了一个数组。我得到的错误是The operand of an increment or decrement operator must be a variable, property or indexer。 idx 会给我需要增加的 bin 编号。我只是不确定增加它的正确方法。感谢您的建议和cmets。

            int binsize = Convert.ToInt32(xrLabel101.Text) / 10;//Max divided by 10 to get the size of each bin
            this.xrChart4.Series[0].Points.Clear();
            int binCount = 10;
            for (int i = 0; i < binCount; i++)
            {
                int m = Convert.ToInt32(xrLabel104.Text);//This is the number of loops needed
                string[] binct = new string[10];

                for (int k = 0; k < m; k++)
                {
                    int idx = Convert.ToInt32(currentcolumnvalue) / binsize;
                    binct[idx]++;//I know this is wrong. Suggestions?
                }

            }

【问题讨论】:

  • binct的数据类型是什么?
  • 我创建了数组来计算 10 个箱子中每个箱子的病例数。它可以只是一个整数。
  • 查看 Tigran 的回答。您正在访问上面代码中索引“idx”处的对象,这是一个字符串数组。您不能增加字符串数组。我假设您也尝试在该 for 循环中使用 k 做某事?
  • k是要排序的事例数。我只是用它来循环适当的次数。

标签: c# arrays increment


【解决方案1】:

这很简单:你的表达式binct[idx]返回的类型不支持数值 像++, + -...

为了避免这种情况,最后有两种方法:

  • Operator overloading
  • 在其他类型上执行相同的操作,然后将结果映射到您想要的类型。

【讨论】:

  • 所以我可以把它改成 int[] binct = new int[10]?
  • @RolandP:如果您将现有数组更改为int[],请确保您可以对其元素进行算术运算,因为它们是整数。
【解决方案2】:

你可以做的是:

            int binsize = Convert.ToInt32(xrLabel101.Text) / 10;//Max divided by 10 to get the size of each bin
            this.xrChart4.Series[0].Points.Clear();
            int binCount = 10;
            for (int i = 0; i < binCount; i++)
            {
                int m = Convert.ToInt32(xrLabel104.Text);//This is the number of loops needed
                int[] binct = new int[10];

                for (int k = 0; k < m; k++)
                {
                    int idx = Convert.ToInt32(currentcolumnvalue) / binsize;
                    binct[idx] = binct[idx] + 1;
                }

            }

【讨论】:

    【解决方案3】:

    你试图增加一个没有意义的字符串。将您的数组改为 int 数组

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      相关资源
      最近更新 更多