Chapter 5

Q1) Suppose we wanted to have a quiz where the question could be about naming the president, the capital city, and so on. How would we do this with multidimensional arrays?

A) We would just make the inner array hold three strings, perhaps like this:

String[][] countriesCitiesAndPresidents;
//now allocate like this
countriesAndCities = new String[5][3];
//and initialize like this
countriesCitiesAndPresidents [0][0] = "United Kingdom";
countriesCitiesAndPresidents [0][1] = "London";
countriesCitiesAndPresidents [0][3] = "Cameron";//at time of writing

Q2) In our persistence example, we saved a continually updating string to a file so that it persisted after the app had been shut down and restarted. This is like asking the user to click on ...

Get Learning Java by Building Android Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.