Arrays
An array is an organized list of data. The data may be int, double, boolean, String or some other data type, even objects. Each piece of data is stored in the list in a numbered position called an index. Indexing starts at 0. For example, if there is an array of names it may be stored like this:
In this example, “Bob” is stored in index position 0, “Babs” is in position 1, “Betty” is in position 2 and so on. Therefore, in an array of size 5 the last data item (in this case “Boyle”) is stored in position 4. Arrays such as the one above are created with statements such as the following:
String[ ] firstNames = new String[5 ];
An integer array of size 15 and indices from 0 to 14 would be created like this:
int[ ] numberList = new int[15];
Note that once the array has been created, its size cannot be changed. Also, all elements of an array must be of the same data type. For example, one array cannot hold both int and String values. To access one of the data items, you must identify its indexed position:
String choice = firstNames[2];
The statement above puts the String Betty into the variable choice. To initialize (or put values into) an array, you can either declare the variables at the time of instantiation like this:
int[ ] numberList = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
or you can create a loop to fill the array:
int[ ] numberList=new int[15];
for (int i = 0; i<15; i++){
numberList[i]=i;
}
A loop can also be used to print out the elements of an array:
for (int i=0; i<15; i++){
System.out.print(numberList[i]);
}
The textbook also gives information on different ways to search an array for particular elements as well as ways to sort an array (alphabetically or numerically). See pages 313-328.
Finally, arrays can be of multiple dimensions:
String[ ] firstNames = new String[5 ];
An integer array of size 15 and indices from 0 to 14 would be created like this:
int[ ] numberList = new int[15];
Note that once the array has been created, its size cannot be changed. Also, all elements of an array must be of the same data type. For example, one array cannot hold both int and String values. To access one of the data items, you must identify its indexed position:
String choice = firstNames[2];
The statement above puts the String Betty into the variable choice. To initialize (or put values into) an array, you can either declare the variables at the time of instantiation like this:
int[ ] numberList = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
or you can create a loop to fill the array:
int[ ] numberList=new int[15];
for (int i = 0; i<15; i++){
numberList[i]=i;
}
A loop can also be used to print out the elements of an array:
for (int i=0; i<15; i++){
System.out.print(numberList[i]);
}
The textbook also gives information on different ways to search an array for particular elements as well as ways to sort an array (alphabetically or numerically). See pages 313-328.
Finally, arrays can be of multiple dimensions:
As practice, try writing a program which takes in 10 numbers from a user and inputs the data into an array. Then print out the numbers in the reverse order from which they were entered. Separate each number with a comma.