Sometimes we might want to store a number of items of the same type, for example a set of chosen lottery numbers would consist of a list of 6 integers. To the unenlightened, we might write this as follows:
int firstNumber = 1;
int secondNumber = 2;
int thirdNumber = 4;
int fourthNumber = 8;
int fifthNumber = 16;
int sixthNumber = 32;
However Java (and most other languages) provide us with the ability to create a variable that holds a number of items - An Array. Consider this line of code which creates a variable to hold an array of ints: int[] lottoNumbers = {1,2,4,8,16,32};
It is similar to the syntax we use to create a normal variable, except the the data type has []
appended to it, and the values are places in braces, separated by commasWe can do a similar thing for other data types, for example Strings:
String[] buildings = {"Sutton", "Ashton", "Dunham", "Hartford", "Sandfield"};
When an array is created its size is determined, in the above example lottoNumbers
it will have a capacity of 6, and buildings
will have a capacity of 5.
Sometimes we may want to declare an array before we know what it's contents are (for example if we wanted to collect the values from the user). We can do this using the following syntax:
int[] intArray = new int[6];
Creating a new array in this format will set all six values to 0 (as it would for other number based data types). A String array would have a series of 'null' objects.
Arrays are accessed by index (a number which represents their position in the array). Unlike conventional numbering, array positions start at 0. The first item in the above array could be accessed as such:
String libraryLocation = buildings[0]; //Sutton
and the fourth item could be accessed as follows:
String deptOfficeLocation = buildings[3]; //Hartford
We can set the value of an item in the same way we read them, by using an index position, for example:
String[] omniumRio2016 = new String[3];
omniumRio2016[0] = "Viviani";
omniumRio2016[1] = "Boudat";
omniumRio2016[2] = "Cavendish";
If we try and access an item in an array using an index greater than or equal to the number of items in the array, an error will occur, for example the following code would cause the application to crash:
String[] jayZsProblems = new String[99];
//code to set Jay-Z's problems goes here...
String someProblem = jayZsProblems[100]; //this would fail
String anotherProblem = jayZsProblems[99]; //this would also fail
Remember that the item at the 99th index would actually be the 100th item (as it the index starts at 0). In the above examples case, the error you would see would be a java.lang.ArrayIndexOutOfBoundsException error. You would also get an error if you tried to set an item at a position that doesn't exist in the array
Because a String is just a sequence of characters, the String class helpfully provides us with a method to convert it to an Array of char elements. Thus we could create an array of characters holding the word 'hello' as follows:
String word = "Greetings";
char[] letters = word.toCharArray();;
We can also convert an Array of characters back to a String as follows:char[] letters = {'h','e','l','l','o'};
String word = new String(letters);
In a new command line application:
Write a programme which will enable the user to enter a multiple choice question, four possible answers, and the number relating to the correct answer. The questions should be stored in a String variable, the four possible answers in an array of Strings, and the correct answer position in an int variable. At this point the programme should prompt the user to clear the console, and then press enter to proceed.
The programme should then print the question and four possible answers to the screen (labelled 1-4), and await input from the user. Once the user has typed a number, the programme should confirm if they are correct, and if not, print the correct answer out.