## CS101 Study Guide

### 7a. Declare and use one-dimensional arrays

• Why do we care about arrays? What are they good for?
• What is the relationship between arrays and objects in Java?
• Which data types can make up the cells of an array?
• Can mixed data types populate the cells of the same array?

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.

### 7b. Create, initialize, and access multi-dimensional arrays

• How are multi-dimensional arrays created, initialized, and accessed?
• Is there a limit to the number of dimensions or the size of any dimension? Why?
• How can nested loops be used to initialize a multidimensional array?
• How can randomly selected in-range integers be used for accessing a multidimensional array?

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 instance: <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.

Review:

### 7c. Use enhanced for-loops to iterate over an arrays' elements

• What are the types of loops that can be used to iterate over an array's elements?
• Once an array of fundamental-type elements is defined, how can we tell how many elements are in it?
• How is it possible to visit each array element without explicitly counting through their total range?
• What are the various ways to initialize an array?

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.

Review:

• Common Array Algorithms: At least: 1, 2, 3, 4, 6, 12, 14, 22, 23, 24, 25. The rest are good examples.
• ArrayLists and Iterators: These are all pretty important. Rarely is there a subunit having only examples. Especially pay attention to classes whose objects hold objects of other classes, and fundamental data types.

### Unit 7 Vocabulary

This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.

• arrays
• cell
• dimensions
• element
• index
• matrix
• ranges
• vector