Arrays 235 the topic of array sizes is a little more

Info icon This preview shows pages 259–262. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays | 235
Image of page 259

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

View Full Document Right Arrow Icon
The topic of array sizes is a little more complex than it first appears, so let’s look at that in more detail. Array Size Arrays know how many elements they contain—several of the previous examples have used the Length property to discover the size of an existing array. This read-only prop- erty is defined by the base Array class, so it’s always present. * That may sound like enough to cover the simple task of knowing an array’s size, but arrays don’t have to be simple sequential lists. You may need to work with multidimensional data, and .NET supports two different styles of arrays for that: jagged and rectangular arrays. Arrays of arrays (or jagged arrays) As we said earlier, you can make an array using any type as the element type. And since arrays themselves have types, it follows that you can have an array of arrays. For ex- ample, suppose we wanted to create a list of forthcoming events over the next five days, grouped by day. We could represent this as an array with one entry per day, and since each day may have multiple events, each entry needs to be an array. Example 7-20 creates just such an array. Example 7-20. Building an array of arrays static CalendarEvent[][] GetEventsByDay(CalendarEvent[] allEvents, DateTime firstDay, int numberOfDays) { CalendarEvent[][] eventsByDay = new CalendarEvent[numberOfDays][]; for (int day = 0; day < numberOfDays; ++day) { DateTime dateOfInterest = (firstDay + TimeSpan.FromDays(day)).Date; CalendarEvent[] itemsOnDateOfInterest = Array.FindAll(allEvents, e => e.StartTime.Date == dateOfInterest); eventsByDay[day] = itemsOnDateOfInterest; } return eventsByDay; } * There’s also a LongLength , which is a 64-bit version of the property, which theoretically allows for larger arrays than the 32-bit Length property. However, .NET currently imposes an upper limit on the size of any single array: it cannot use more than 2 GB of memory, even in a 64-bit process. So in practice, LongLength isn’t very useful in the current version of .NET (4). (You can use a lot more than 2 GB of memory in total in a 64-bit process—the 2 GB limit applies only to individual arrays.) 236 | Chapter 7: Arrays and Lists
Image of page 260
We’ll look at this one piece at a time. First, there’s the method declaration: static CalendarEvent[][] GetEventsByDay(CalendarEvent[] allEvents, DateTime firstDay, int numberOfDays) { The return type— CalendarEvent[][] —is an array of arrays, denoted by two pairs of square brackets. You’re free to go as deep as you like, by the way—it’s perfectly possible to have an array of arrays of arrays of arrays of anything. The method’s arguments are fairly straightforward. This method expects to be passed a simple array containing an unstructured list of all the events. The method also needs to know which day we’d like to start from, and how many days we’re interested in.
Image of page 261

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

View Full Document Right Arrow Icon
Image of page 262
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