Part III
Arrays and Strings
IntroductionArrays are like variables, except that they allow you to store more than one value in a given variable while only using one reference. Imagine a variable as a box that can hold a certain type. An array would be the equivalent of that same box divided into multiple pieces, each piece containing a piece of data. Of course, since the array box stores more information than the single variable box, it is bigger, so it uses more memory.
Array IndexesWe can use the same name to get the value of a part in the array, but we need a way to differentiate between each
cell, or part, of the array. To do this, we use a
zero-index in the array. Let's say we have an array that's 100 cells large. The zero-index, as you might assume, starts at zero, so our first value is
myArray[0], and the 100
th value is
myArray[99]. The fifth value is
myArray[4], so think of it as myArray[
item number - 1].
Creating ArraysArrays are declared similarly to regular variables, except we specify how many array cells we want to use. Unlike the zero-index, when you create the array, you specify exactly how many cells you want to use, i.e.
new myArray[10] for an array named
myArray 10 cells long.
Using Array CellsOnce you have declared an array, you can use an array index to both store and get information stored in that cell. Right now, we can only store integers and floats in our arrays. Strings and characters will come later. Let's say we want to, like our last example, set the payrolls of each department into an array, with payrollArray[0] reserved for department 1, payrollArray[1] for department 2, etc. First, instantiate (create) a new array with
new payrollArray[5];Given that we know each department's payroll is $10,000 times the department ID, we can use our mathematical operators, a
for loop, and our array. We've declared our array, so now let's create our
for loop to iterate through each department, starting at 1 and ending at 5.
// Declare our new array
new payrollArray[5];
// Iterate through each department.
for(new i = 1; i < 5; i++)
{
// <code here>
}
However, this still doesn't store each department's payroll in the array. Trying to compile this will result in an array something like
payrollArray is unused. Since our array starts at index 0 while our department IDs start at 1, we can subtract 1 from the department ID to get the array index that goes with it. Array indexes, as long as they're integral, are valid, so mathematical expressions can also be array indexes, like so:
payrollArray[i - 1]; with
i - 1 being the array index (DepartmentID - 1). If
i was 1, the index would be 0, which is valid, so let's use this to our advantage.
We also know that our payroll is $10,000 times the department ID, so whatever the cell is, it will be equal to the
department ID * 10000 (note that there are NO commas in numbers).
// Declare our new array.
new payrollArray[5];
// Iterate through each department.
for(new i = 1; i < 5; i++)
{
// i - 1 = DepartmentID - 1, for our zero-indexed array
payrollArray[i - 1] = i * 10000; // Department ID * 10000
}
We can now get the payroll from this array by using
payrollArray[ID - 1]StringsA
string is simply an array of characters (letters and punctuation) which, when put together, form a sentence, phrase, word, etc. Since they are arrays, they work similar to integral arrays, except each cell is one letter or punctuation mark. In addition, the last character in the string has to be a null character (\0), which tells the computer when the string ends. Strings can be as long or longer than the sentence that they hold. For example, the following sentence is 12 characters long:
I like pie.It consists of 11 letters and punctuation marks, plus the null character at the end.
myString[12] would work for it.
myString[25] would work too. However,
myString[11] is invalid. Each cell is going to be one letter, thus:
myString[0] = "I"myString[1] = " "myString[4] = "k"Strings can also be formatted using the
format() function, which will be described in further detail later. Entire strings can be created at once as well. If we want to use our "I like pie." sentence for our string, we first have to declare our new array.
new myString[12];Instead of saving to individual cells, we can instead reference just the variable, then place the sentence inside a string, enclosed by double quotes (
""). For example:
myString = "I like pie."This will automatically include the null character at the end.