编译器错误:无效的排名说明符:预期','或']关于二维数组初始化

发布于 2024-10-07 04:59:55 字数 642 浏览 0 评论 0原文

我有一堂课的作业是用 C# 完成的。作为一个完全的 C# 新手,我首先用 Java 完成了这个项目,现在我正在尝试将其转换为 C#。我有以下函数,会导致以下编译器错误。

错误:无效的排名说明符:预期','或']'在以下行:

int[][] grid=new int[g.cols][g.rows];

Visual studio在g.rows中强调了g

public int[][] getConvergenceCounts(MandelbrotGrid g){
  int[][] grid=new int[g.cols][g.rows];

  for(int x=0;x<g.cols;x++){
     for(int y=0;y<g.rows;y++){
        double tx=x*(double)3/400-1.5;
        double ty=y*(double)3/400-1.5;
        grid[x][y]=getConvergenceCount(new Complex(ty,tx));
     }
  }

  return grid;
}

我不知道我在这里做错了什么并阅读在 C# 中的多维数组上似乎没有帮助。

I have an assignment for a class that is to be done in C#. Being a complete C# newbie, I did the project in Java first and I'm now trying to convert it to C#. I have the following function which results in the following compiler error.

Error: Invalid rank specifier: expected',' or ']' on the following line:

int[][] grid=new int[g.cols][g.rows];

Visual studio is underlining the g in g.rows

public int[][] getConvergenceCounts(MandelbrotGrid g){
  int[][] grid=new int[g.cols][g.rows];

  for(int x=0;x<g.cols;x++){
     for(int y=0;y<g.rows;y++){
        double tx=x*(double)3/400-1.5;
        double ty=y*(double)3/400-1.5;
        grid[x][y]=getConvergenceCount(new Complex(ty,tx));
     }
  }

  return grid;
}

I have no idea what I'm doing wrong here and reading up on Multidimensional arrays in C# didn't seem to help.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-10-14 04:59:55

C# 编译器认为您正在尝试声明锯齿状数组,但这样做是错误的。交错数组是一个数组的数组,其中主数组中包含的每个数组可以具有不同数量的元素。交错数组声明如下:

int[][] jaggedArray = new int[numElements][];

这将创建一个可以在其中保存“numElements”整数数组的数组。

您想要声明一个多维数组,例如:

int[,] grid = new int[g.cols, g.rows];

The C# compiler thinks you're trying to declare a jagged array, and doing so incorrectly. A jagged array is an array of arrays, where each array contained within the main array can have a different number of elements. A jagged array is declared as follows:

int[][] jaggedArray = new int[numElements][];

Which would create an array that could hold "numElements" arrays of integers within it.

You want to declare a multidimensional array, e.g.:

int[,] grid = new int[g.cols, g.rows];
硬不硬你别怂 2024-10-14 04:59:55
public int[][] getConvergenceCounts(MandelbrotGrid g){
    int[][] grid=new int[g.cols][];

    for(int x=0;x<g.cols;x++){
     int[x] = new int[g.rows]
     for(int y=0;y<g.rows;y++){
        double tx=x*(double)3/400-1.5;
        double ty=y*(double)3/400-1.5;
        grid[x][y]=getConvergenceCount(new Complex(ty,tx));
     }
  }

  return grid;
}
public int[][] getConvergenceCounts(MandelbrotGrid g){
    int[][] grid=new int[g.cols][];

    for(int x=0;x<g.cols;x++){
     int[x] = new int[g.rows]
     for(int y=0;y<g.rows;y++){
        double tx=x*(double)3/400-1.5;
        double ty=y*(double)3/400-1.5;
        grid[x][y]=getConvergenceCount(new Complex(ty,tx));
     }
  }

  return grid;
}
你是年少的欢喜 2024-10-14 04:59:55

如果您想使用锯齿状数组,@Frank 的解决方案就是您需要执行的方法。启动锯齿状数组时不能声明两个维度​​,因为 C# 假设​​行的维度不相等。在 @Doughnut 的解决方案中,如果您有矩阵类型解决方案(就是这样),那么多维数组方法是一个很好的解决方案,但是 C# 针对单维数组进行了优化,您可能仍然希望使用锯齿状数组选项以节省时间成本。因此,如果您要对多维数组执行许多操作,您应该在输入行时初始化一个锯齿状数组,并单独指定行长度。

public int[][] getConvergenceCounts(MandelbrotGrid g)
{
    int[][] grid=new int[g.cols][];

    for(int x=0;x<g.cols;x++){
        grid[i] = new int[g.rows];
        for(int y=0;y<g.rows;y++){
           double tx=x*(double)3/400-1.5;
           double ty=y*(double)3/400-1.5;
           grid[x][y]=getConvergenceCount(new Complex(ty,tx));
         }
    }
    return grid;
}

If you want to use a jagged array the solution by @Frank is the method you need to do. You cannot declare both dimensions when you initiate a jagged array because the C# assumption is that your rows will have unequal dimensions. In @Doughnut's solution the multidimensional array method is a good solution if you have a matrix type solution (which this is), however C# is optimized for single dimensional arrays and you may still want to use the jagged array option in order to preserve time costs. For this reason, if you will be performing many operations on your multidimensional array you should initialize a jagged array THEN as you input your rows, specifying the row length individually.

public int[][] getConvergenceCounts(MandelbrotGrid g)
{
    int[][] grid=new int[g.cols][];

    for(int x=0;x<g.cols;x++){
        grid[i] = new int[g.rows];
        for(int y=0;y<g.rows;y++){
           double tx=x*(double)3/400-1.5;
           double ty=y*(double)3/400-1.5;
           grid[x][y]=getConvergenceCount(new Complex(ty,tx));
         }
    }
    return grid;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文