【问题标题】:How to search through a jagged array如何搜索锯齿状数组
【发布时间】:2021-11-07 16:03:41
【问题描述】:

此代码应在小数数组中搜索指定范围内的元素,并返回与范围条件匹配的元素的出现次数。

问题是我在访问锯齿状数组时遇到问题,我的代码:

public static int GetDecimalsCount(decimal[] arrayToSearch, decimal[][] ranges)
{
    if (arrayToSearch is null)
    {
        throw new ArgumentNullException(nameof(arrayToSearch));
    }
    else if (ranges is null)
    {
        throw new ArgumentNullException(nameof(ranges));
    }
    else
    {
        int sum = 0;
        for (int i = 0; i < arrayToSearch.Length; i++)
        {
            for (int j = 0; j < ranges.Length; j++)
            {
                for (int n = 0; n < ranges[j].Length; n++)
                {
                    if (arrayToSearch[i] >= ranges[j][n] && arrayToSearch[i] <= ranges[j][n + 1])
                    {
                        sum++;
                    }
                }
            }
        }

        return sum;
    }
}

范围是从最低到最高,所以它总是两位小数的数组

我也相信这至少应该有效:

if (arrayToSearch[i] >= ranges[j][0] && arrayToSearch[i] <= ranges[j][1])

怎么不比较数组,我不懂。

编辑 1:来自测试的数组中的数据

插入一些测试代码,如果需要完整的我可以发送。它有点长,并且有其他任务的不重要的测试用例。

private static readonly decimal[] ArrayWithFiveElements = { 0.1m, 0.2m, 0.3m, 0.4m, 0.5m };
private static readonly decimal[] ArrayWithFifteenElements = { decimal.MaxValue, -0.1m, -0.2m, decimal.One, -0.3m, -0.4m, -0.5m, decimal.Zero, 0.1m, 0.2m, 0.3m, 0.4m, 0.5m, decimal.MinusOne, decimal.MinValue };

[Test]
public void DecimalCounter_FiveElementsOneRange_ReturnsResult()
{
    // Arrange
    decimal[][] ranges =
    {
        new[] { 0.1m, 0.2m },
    };

    // Act
    int actualResult = DecimalCounter.GetDecimalsCount(DecimalCounterTests.ArrayWithFiveElements, ranges);

    // Assert
    Assert.AreEqual(2, actualResult);
}

[Test]
public void DecimalCounter_FiveElementsTwoRanges_ReturnsResult()
{
    // Arrange
    decimal[][] ranges =
    {
        new[] { 0.1m, 0.2m },
        new[] { 0.4m, 0.5m },
    };

    // Act
    int actualResult = DecimalCounter.GetDecimalsCount(DecimalCounterTests.ArrayWithFiveElements, ranges);

    // Assert
    Assert.AreEqual(4, actualResult);
}

【问题讨论】:

  • 怎么不比较数组:会发生什么? - ranges[n][j] 你是说ranges[j][n] 吗?
  • 另外,因为您访问ranges[j][n + 1],所以您必须将最内层的循环变量限制为n &lt; ranges[j].Length - 1(带有附加的- 1)。否则[n + 1] 将尝试访问超出范围的元素。
  • 如果范围总是由两个数字组成,我会使用元组数组而不是锯齿状数组。:(decimal start, decimal end)[] = { (0.1m, 0.2m), (0.4m, 0.5m) }; 使用if (arrayToSearch[i] &gt;= ranges[j].start &amp;&amp; arrayToSearch[i] &lt;= ranges[j].end) 进行测试
  • 这应该可以工作:if (arrayToSearch[i] &gt;= ranges[j][0] &amp;&amp; arrayToSearch[i] &lt;= ranges[j][1]) 当然你不需要最里面的循环(第三个循环)。为什么你认为它不起作用? Debug your code 看看会发生什么。
  • 你试过调试器吗,这里选择的工具是..?

标签: c# arrays loops jagged-arrays


【解决方案1】:

修复 cmets 中指出的错误后,我在代码中没有发现任何问题; https://dotnetfiddle.net/F6Yjy0

public static int GetDecimalsCount(decimal[] arrayToSearch, decimal[][] ranges)
{
    if (arrayToSearch == null)
    {
        throw new ArgumentNullException(nameof(arrayToSearch));
    }
    else if (ranges == null)
    {
        throw new ArgumentNullException(nameof(ranges));
    }
    else
    {
        int sum = 0;
        for (int i = 0; i < arrayToSearch.Length; i++)
        {
            for (int j = 0; j < ranges.Length; j++)
            {
                //for (int n = 0; n < ranges[j].Length; n++)
                //{
                    if (arrayToSearch[i] >= ranges[j][0] && arrayToSearch[i] <= ranges[j][1])
                    {
                        sum++;
                    }
                //}
            }
        }

        return sum;
    }
}

最里面的循环是毫无意义的;它只运行一次,可以用索引 0/1 替换。删除它也消除了 OOB 问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2020-04-02
    • 1970-01-01
    • 2015-09-23
    • 2023-03-15
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多