简体   繁体   English

将2D阵列更改为锯齿状阵列

[英]Changing a 2D array to jagged array

In my code, I've using a 2D multidimensional array to represent a grid (not always of equal sizes, eg 10x15 or 21x7). 在我的代码中,我使用了2D多维数组来表示网格(并不总是具有相等的大小,例如10x15或21x7)。 After reading about how jagged arrays are faster and are generally considered better, I decided I would change my 2D array into a jagged array. 在阅读了锯齿状阵列如何更快并且通常被认为更好之后,我决定将2D阵列更改为锯齿状阵列。

This is how I declared the multidimensional array: 这就是我声明多维数组的方式:

int[,] array = new int[10, 10];

I'm trying to figure out how to declare and then initialise the same thing, but using jagged arrays. 我试图弄清楚如何声明然后初始化同一件事,但使用锯齿状数组。

Edit This code is inside a class, and in the constructor I already have: 编辑此代码在类内部,并且在构造函数中我已经具有:

class ProceduralGrid
{
    private int[][] grid;

    private int _columns;
    private int _rows;

    public ProceduralGrid(int rows, int columns)
    {
        _rows = rows;             //For getters
        _columns = columns;

        //Create 2D grid
        int x, y;
        grid = new int[rows][];

        for (x = 0; x < grid.Length; x++)
        {
            grid[x] = new int[10];
        }
    }

    public int GetXY(int rows, int columns)
    {
        if (rows >= grid.GetUpperBound(0) + 1)
        {

            throw new ArgumentException("Passed X value (" + rows.ToString() +
                ") was greater than grid rows (" + grid.GetUpperBound(0).ToString() + ").");
        }
        else
        {
            if (columns >= grid.GetUpperBound(1) + 1)
            {

                throw new ArgumentException("Passed Y value (" + columns.ToString() +
                    ") was greater than grid columns (" + grid.GetUpperBound(1).ToString() + ").");
            }
            else
            {
                return grid[rows][columns];
            }
        }
    }
}

And in another method I'm simply doing: 在另一种方法中,我只是在做:

    Console.WriteLine(grid.GetXY(5, 5).ToString());

Error message I'm getting: 我收到错误消息:

Unhandled Exception: System.IndexOutOfRangeException: Array does not have that m
any dimensions.
   at System.Array.GetUpperBound(Int32 dimension)
   at ProcGen.ProceduralGrid.GetXY(Int32 rows, Int32 columns) in C:\Users\Lloyd\
documents\visual studio 2010\Projects\ProcGen\ProcGen\ProceduralGrid.cs:line 115
   at ProcGen.Program.Main(String[] args) in C:\Users\Lloyd\documents\visual stu
dio 2010\Projects\ProcGen\ProcGen\Program.cs:line 27

What am I doing wrong and how should I be doing it? 我在做什么错,应该怎么做?

Since you're dealing with one-dimensional arrays, you can simply use the Length Property to get the length of the first dimension: 由于要处理一维数组,因此可以简单地使用Length属性来获取第一维的长度:

int[][] grid = new int[10][];

for (int x = 0; x < grid.Length; x++)
{
    grid[x] = new int[10];
}

(Using the GetLength Method works as well:) (使用GetLength方法也可以:)

int[][] grid = new int[10][];

for (int x = 0; x < grid.GetLength(0); x++)
{
    grid[x] = new int[10];
}

The problem with your code is that you're calling grid.GetUpperBound(1) where grid is a one-dimensional array -- it doesn't have a second dimension (index 1) that you could get the upper bound of. 代码的问题在于,您正在调用grid.GetUpperBound(1) ,其中grid是一维数组-它没有第二个维度(索引1),可以获取上限。

Your GetXY method should look like this: 您的GetXY方法应如下所示:

public int GetXY(int x, int y)
{
    if (x < 0 || x >= grid.Length)
    {
        throw ...
    }

    int[] items = grid[x];

    if (y < 0 || y >= items.Length)
    {
        throw ...
    }

    return items[y];
}

Note that jagged arrays are not magic that makes your code faster – measure if they actually do! 请注意,锯齿状的数组并不是魔术,它可以使您的代码更快-测量它们是否确实如此!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM