The syntax for accessing elements in a rectangular

Info icon This preview shows pages 265–268. Sign up to view the full content.

View Full Document Right Arrow Icon
The syntax for accessing elements in a rectangular array is slightly different from that of a jagged array. But like a jagged array, the access syntax is consistent with the dec- laration syntax—as Example 7-22 shows, we use a single pair of square brackets, pass- ing in an index for each dimension, separated by commas. Arrays | 241
Image of page 265

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Figure 7-6. A two-dimensional rectangular array Example 7-22. Accessing an element in a rectangular array static bool CanCharacterMoveDown(int x, int y, int[,] walls) { int newY = y + 1; // Can't move off the bottom of the map if (newY == walls.GetLength(0)) { return false; } // Can only move down if there's no wall in the way return walls[newY, x] == 0; } If you pass in the wrong number of indexes, the C# compiler will com- plain. The number of dimensions (or rank , to use the official term) is considered to be part of the type: int[,] is a different type than int[,,] , and C# checks that the number of indexes you supply matches the array type’s rank. 242 | Chapter 7: Arrays and Lists
Image of page 266
Example 7-22 performs two checks: before it looks to see if there’s a wall in the way of the game character, it first checks to see if the character is up against the edge of the map. To do this, it needs to know how big the map is. And rather than assuming a fixed-size grid, it asks the array for its size. But it can’t just use the Length property we saw earlier—that returns the total number of elements. Since this is a 12 × 12 array, Length will be 144. But we want to know the length in the vertical dimension. So instead, we use the GetLength method, which takes a single argument indicating which dimen- sion you want—0 would be the vertical dimension and 1 in this case is horizontal. Arrays don’t really have any concept of horizontal and vertical. They simply have as many dimensions as you ask for, and it’s up to your program to decide what each dimension is for. This particular program has chosen to use the first dimension to represent the vertical position in the maze, and the second dimension for the horizontal position. This rectangular example has used a two-dimensional array of integers, and since int is a value type, the values get to live inside the array. You can also create multidimen- sional rectangular arrays with reference type elements. In that case, you’ll still get a single object containing all the elements of the array in all their dimensions, but these individual elements will be null references—you’ll need to create objects for them to refer to, just like you would with a single-dimensional array. While jagged and rectangular multidimensional arrays give us flexibility in terms of how to specify the size of an array, we have not yet dealt with an irritating sizing problem mentioned back at the start of the chapter: an array’s size is fixed. We saw that it’s possible to work around this by creating new arrays and copying some or all of the old data across, or by getting the Array.Resize method to do that work for us. But these are inconvenient solutions, so in practice, we rarely work directly with arrays in C#.
Image of page 267

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 268
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern