错误CS1061:类型`System.Collections.Generic.List>'不包含“GetLength”的定义;并且没有扩展 mthd

发布于 2025-01-11 21:16:49 字数 1335 浏览 5 评论 0 原文

我用 C# 编码并得到

不包含 GetLength' 的定义,并且没有类型的扩展方法 GetLength'

在 HACKERRANK 中没有类型的扩展方法 GetLength'...相同的代码是可执行的([并给出正确答案][1] )在 Visual Studio 2019 上运行,但不适用于 HackerRank 编译器。在 HackerRank 和 HackerRank 上使用数组时,我多次遇到此问题。我使用了Count ...但是在二维数组Count不起作用。

public static int diagonalDifference(List<List<int>> arr)
{
        int sumRight=0;
        int sumLeft=0;
        //sum of left Diagonal
        for(int i=0;i<arr.GetLength(0);i++)
        {
            for(int j=i;j<arr.GetLength(1);j++)
            {
                sumRight = sumRight + arr[i, j];
                break;
            }
        }
        //Console.WriteLine(sumRight);
        
        //Sum of Left diagonal
        int p = -1;
        for (int i=arr.GetLength(1)-1;i>=0;i--)
        { 
            p = p + 1;
            for(int j=p;j<arr.GetLength(0);j++)
            {
                sumLeft = sumLeft + arr[j, i];
                break;
            }             
        }
        //Console.WriteLine(sumLeft);
        
        //calculate diagonal difference
        int absolute = sumRight - sumLeft;
        int abpos = Math.Abs(absolute);
        return abpos;

}

im coding in C# and getting

does not contain a definition for GetLength' and no extension method GetLength' of type

in HACKERRANK...the same code is executable([and giving correct answer][1]) on Visual Studio 2019 and not working on HackerRank Compiler.I have this problem several time while working with arrays on HackerRank & I used Count ...But in 2D Array the Count didn't work.

public static int diagonalDifference(List<List<int>> arr)
{
        int sumRight=0;
        int sumLeft=0;
        //sum of left Diagonal
        for(int i=0;i<arr.GetLength(0);i++)
        {
            for(int j=i;j<arr.GetLength(1);j++)
            {
                sumRight = sumRight + arr[i, j];
                break;
            }
        }
        //Console.WriteLine(sumRight);
        
        //Sum of Left diagonal
        int p = -1;
        for (int i=arr.GetLength(1)-1;i>=0;i--)
        { 
            p = p + 1;
            for(int j=p;j<arr.GetLength(0);j++)
            {
                sumLeft = sumLeft + arr[j, i];
                break;
            }             
        }
        //Console.WriteLine(sumLeft);
        
        //calculate diagonal difference
        int absolute = sumRight - sumLeft;
        int abpos = Math.Abs(absolute);
        return abpos;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文