You may have heard the phrase "data driven applications". Broadly, it refers to the vast volumes of data that are available in the world today. It is possible to build time-dense "sensors" that gather data at a minute level. This data can be used to guide business and industrial activity to increase profitability. However, gathering data itself is useless if you can only listen and look. You have to also hear and see. To do that, you have to process your data. That processing requires the organization of data in some useful way. As in all things, there are ways and there are ways. In this unit we discuss arrays, a means of organizing data into multidimensional blocks.
Data of any single type can be organized into an array. Arrays can be of only one dimension (a vector) or multiple dimensions (a matrix). In matrix algebra, even a single variable can be thought of as a 1x1 matrix. It may be difficult to imagine matrices of higher than three dimensions. But, think of a room of rooms filled with chests of drawers and cubby-holes in each drawer, each cubby-hold containing a specific item.
In Java, arrays are objects. As in other languages, each cell (element) of an object (data of some type) can be operated on just like a single variable of that type. So, for instance, integers can be multiplied or object attributes and methods can be accessed. The idea of arrays containing objects can be very powerful since an object's class definition can contain variables of any type, including arrays. Thus, we can have arrays of arrays, each with their own independence and data element values.
Review Arrays: Arithmetic Expressions: At least read sections 2, 3, 4, 5, 15.
The ability to work with arrays beyond three dimensions greatly expands your potential for building programs that deal with situations of considerable complexity. Java sets no limits on the number of dimensions. Only available memory limits the size of each dimension. Thus, Java is positioned to enable solutions to problems not yet encountered. It "expands" to meet the situation at hand.
A good general approach to defining a multidimensional array is:
<data type><space><variable name>[ ]...[ ] = new <data type>[<size dimension 1>]...[<size dimension n>];
The size values must be an integer greater than 0.
Notice that the above approach works well for fundamental data types. However, more is needed if the data type is a class description. Each cell in the array needs to have an object created for it. Otherwise, the cells only contain a null pointer. For
<variable name>[<index>] = new <data type>();
Accessing every cell in an array requires nested loops, the number of loops being equal to the number of dimensions. Any kind of loop will do. This concept is illustrated by the following figure:
Of course, it is also possible to access arrays using random indexes within the ranges of the various dimensions. You do not have to arrive at index values only by looping. Choose the method appropriate to the situation and the language. Recall that Java uses a 0-based indexing system (array indexes start with 0), whereas other languages use a 1-based system (array indexes start with 1). Keep that in mind as you proceed.
Once data is organized and we know how to access that data, we can move on to doing something useful with that data. As we have seen, one can use various types of loops to count through all the elements of an array. Or, we can access those elements randomly, or in some order appropriate to the application at hand.
What application is that? Use your imagination. What are you trying to accomplish? What do you want this tool thing, this computer, to do for you? Set about developing your vision, objectives, tasks. Then begin carrying out those tasks to reach measurable milestones identified by your project schedule. Schedule, budget, and accountability are needed for accomplishment to take place. In this way, the computer, limited as it is, can be a very useful tool, a blackbox that can become what you need it to be, within the limits of its fundamental capabilities.
The lessons associated with this learning objective describe several fairly common activities that computers are asked to perform. They serve as examples for how to write code for similar activities in the Java language. In our review questions, we explore other activities that will interest you.
This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.