Entering the nth dimension with arrays
We very briefly mentioned that an array can even hold other arrays at each position. And of course, if an array holds lots of arrays that in turn hold lots of some other type, how do we access the values in the contained arrays? And why would we ever need this anyway? Look at this next example to see where multidimensional arrays can be useful.
Multidimensional array mini app
Let's make a simple multidimensional array example. You can get the working project for this example on the GitHub repo. It is in Chapter 12/Multidimensional Array Example/MainActivity.java
.
Create a project with an empty activity
and call it Multidimensional Array Example
. Leave the activity name as the default; it is not important.
After the call to super.onCreate…
in the onCreate
method, declare and initialize a two-dimensional array like this:
// Random object for generating question numbers Random randInt = new Random(); // a variable to hold...