Multidimensional Arrays 10 17 2007 1 Opening Discussion Let s look at some solutions to the interclass problem Do you have any questions about the assignment ACM s Make the Most of Your Major event is this evening Fun things in planetary science Lots of Cassini data on rings and moons Nice model of planetary formation 2 Searching Far more common than sorting is the act of searching If I give you an array of ints and ask you to tell me where the number 33 is what would you do How fast is this procedure How many checks do you need to do 3 Binary Search The real benefit of sorting is that we can search faster If data is sorted we can do a binary search Start in the middle and determine if what you are looking for is on the right or the left This only works for sorted data but it is MUCH faster for big data sets It is O log2 n instead of O n 4 Array Types When we declare int a you can think of it as a being of type int That is actually the accepted syntax in Java The array types are themselves types By adding the brackets you basically create a new type that is an array of the base type You can do this with any type 5 Multidimensional Arrays If array types are types and we can make arrays of any type then we can make arrays of arrays This is what produces multidimensional arrays To declare a multidimensional array simply put multiple sets of brackets with sizes after the variable name int a 10 20 When you pass a multidimensional array you must specify the size of every dimension except the first one You can put lots of dimensions on an array but there are few applications needed more than three Memory requirements grow quickly 6 Mazes There are lots of different uses for 2 D arrays but one of my favorite is that of mazes These also provide a great playground for recursion because maze functions are among the set that are hard to write with loops but easy to do with recursion 7 Minute Essay Given that an int takes four bytes on these machines how much memory would the following declaration consume int a 10 5 20 100 Remember that the assignment is due on Friday ACM s Make the Most of Your Major event is this evening at 7pm in HAS 340 Interclass Problem Write a function that tells you the length of the longest path from a given location to 0 0 on a maze made with a 2 D array of ints 8
View Full Document