为什么此算法中的数组索引超出范围?

用户名

所以我在下面的注释代码中做了一些不言自明的练习

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{

    public static int[,] GetPairs ( int [] arr ) 
    {
        // given an array arr of unique integers, returns all the pairs
        // e.g. GetPairs(new int [] { 1, 2, 3, 4, 5 }) would return
        // { {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5} }   

        int n = (arr.Length * (arr.Length - 1))/2; // number of pairs unique pairs in an array of unique ints
        if ( n < 1 ) return new int[0,2] {}; // if array is empty or length 1
        int[,] pairs = new int[n,2]; // array to store unique pairs
        // populate the pairs array:
        for ( int i = 0, j = 0; i < arr.Length; ++i ) 
        {
            for ( int k = i + 1; k < arr.Length; ++k )
            {
                pairs[j,0] = arr[i];
                pairs[j,1] = arr[k];
                ++j;
            }
        }
        return pairs;       
    }

    public static void Main()
    {
        int [] OneThroughFour = new int [4] { 1, 2, 3, 4 };
        int [,] Pairs = GetPairs(OneThroughFour);
        for ( int i = 0; i < Pairs.Length; ++i )
        {
            Console.WriteLine("{0},{1}",Pairs[i,0],Pairs[i,1]);
        }

    }
}

我得到的错误是

[System.IndexOutOfRangeException:索引超出数组的范围。

在循环

    for ( int i = 0; i < Pairs.Length; ++i )
    {
        Console.WriteLine("{0},{1}",Pairs[i,0],Pairs[i,1]);
    }

这对我来说毫无意义。什么是越界?当然不是i,因为它的范围是01,..., Pairs.Length - 1对于那些有效索引,肯定不是01

另外,是否有可能比O(n^2).NET更好呢?.NET是否有一种更紧凑,更有效的方法?

雅库布·马萨德(Yacoub Massad)

对于二维数组,该Length属性返回第一维的长度乘以第二维的长度。在您的情况下,这等于2 * n

据我所知,您想要遍历第一维。

使用如下GetUpperBound方法:

for (int i = Pairs.GetLowerBound(0); i <= Pairs.GetUpperBound(0); ++i)
{
    //...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么此变量超出范围?

来自分类Dev

为什么索引超出范围

来自分类Dev

为什么光标索引超出范围?

来自分类Dev

为什么索引超出范围?

来自分类Dev

为什么索引超出范围?

来自分类Dev

为什么索引超出范围?(Python)

来自分类Dev

Swift中的数组索引超出范围

来自分类Dev

为什么调用此 lambda 命令时字符串索引超出范围?

来自分类Python

为什么此迭代列表增长代码使IndexError:列表分配索引超出范围?

来自分类Dev

IndexError:列表索引超出范围:我可以理解为什么会发生此错误

来自分类Dev

为什么运行此程序时出现“字符串索引超出范围”错误?

来自分类Dev

为什么此代码会产生“索引超出范围”异常?

来自分类Dev

Python 列表列表为什么会出现此错误:IndexError:列表索引超出范围?

来自分类Dev

为什么在列表片上的任何超出范围的索引中添加:都不会超出范围?

来自分类Dev

数组索引超出范围

来自分类Dev

选择排序算法中的索引超出范围

来自分类Dev

为什么这段代码会超出范围生成数组索引?

来自分类Dev

为什么此向量向量超出范围?

来自分类Dev

Python列表索引超出范围-算法

来自分类Dev

为什么我收到“列表索引超出范围”的错误

来自分类Dev

为什么我的索引超出范围异常

来自分类Dev

为什么我的索引超出范围异常?

来自分类Java

索引超出范围-我不明白为什么

来自分类Dev

为什么此列表索引超出范围

来自分类Dev

为什么List IndexOf允许超出范围的起始索引?

来自分类Dev

为什么我的字符串索引超出范围?

来自分类Dev

为什么由于索引超出范围而导致致命错误?

来自分类Dev

为什么显示列表索引超出范围错误?

来自分类Dev

为什么会出现错误:列表索引超出范围

Related 相关文章

  1. 1

    为什么此变量超出范围?

  2. 2

    为什么索引超出范围

  3. 3

    为什么光标索引超出范围?

  4. 4

    为什么索引超出范围?

  5. 5

    为什么索引超出范围?

  6. 6

    为什么索引超出范围?(Python)

  7. 7

    Swift中的数组索引超出范围

  8. 8

    为什么调用此 lambda 命令时字符串索引超出范围?

  9. 9

    为什么此迭代列表增长代码使IndexError:列表分配索引超出范围?

  10. 10

    IndexError:列表索引超出范围:我可以理解为什么会发生此错误

  11. 11

    为什么运行此程序时出现“字符串索引超出范围”错误?

  12. 12

    为什么此代码会产生“索引超出范围”异常?

  13. 13

    Python 列表列表为什么会出现此错误:IndexError:列表索引超出范围?

  14. 14

    为什么在列表片上的任何超出范围的索引中添加:都不会超出范围?

  15. 15

    数组索引超出范围

  16. 16

    选择排序算法中的索引超出范围

  17. 17

    为什么这段代码会超出范围生成数组索引?

  18. 18

    为什么此向量向量超出范围?

  19. 19

    Python列表索引超出范围-算法

  20. 20

    为什么我收到“列表索引超出范围”的错误

  21. 21

    为什么我的索引超出范围异常

  22. 22

    为什么我的索引超出范围异常?

  23. 23

    索引超出范围-我不明白为什么

  24. 24

    为什么此列表索引超出范围

  25. 25

    为什么List IndexOf允许超出范围的起始索引?

  26. 26

    为什么我的字符串索引超出范围?

  27. 27

    为什么由于索引超出范围而导致致命错误?

  28. 28

    为什么显示列表索引超出范围错误?

  29. 29

    为什么会出现错误:列表索引超出范围

热门标签

归档