Introduction to Arrays
[comp1110-71] - Fabian Muehlboeck
Introduction to Arrays
[comp1110-71]- Fabian Muehlboeck
Definition 1. Arrays
[comp1110-15] - Liam O'Connor
Definition 1. Arrays
[comp1110-15]- Liam O'Connor
Arrays are a primary way to store linear, ordered collections of data. The elements of an array must all have the same type. An array type is written by appending [] to the type of the array elements. For example, the type of an array of int values is written as int[], whereas the type of an array of String values is written String[]. We create an array with the new keyword, like so:
The above code makes space in memory (called allocating) for a sequence of 50 int values, and stores the location of that space in memory in the newly-created variable arr.
By default, all ints in our array will be set to zero. If we wish to initialise our array with other values, we can include them in the new invocation like so:
We can access the contents of an array by index. The ith element of the array a (starting counting at zero) can be retreived using the notation a[i], for example:
We can also update elements of an array using this notation:
To get the length of an array a, we can write a.length:
Warning.
- Liam O'Connor
Warning.
- Liam O'Connor
For Strings, we write str.length() (i.e. calling a method, with the parentheses), but for arrays, we just write arr.length.
Example 2. Printing Array Elements (In reverse order)
[comp1110-72] - Fabian Muehlboeck
Example 2. Printing Array Elements (In reverse order)
[comp1110-72]- Fabian Muehlboeck
In the lecture, we went through the following example of looping through an array.
Note that, in contrast to earlier, we start our counter variable at the end (i = arr.length),
and reduce it before we use it in the body of the loop.
It is always a good idea to sanity-check your indices to prevent off-by-one errors (which would crash your program). Often, it suffices to look at the first and last index values.
In this case, i starts as arr.length, but the first valid index from the back is arr.length - 1.
Since we assign i to be i - 1 before looking at the array, that works out fine.
For the last index, the loop will end when i == 0, so the last time it could run is when i == 1.
In that case, because we again reduce i before accessing the array, the last index we will look at is 0,
which is both a valid index if the array is not empty (in which case, we would not be in the loop to begin with),
and also the last index we want to look at.
Finally, the loop invariant (§ [comp1110-0W]) tracks the property that we wanted, which is that all elements of the array within a certain range have been printed, in such a way that the range coincides with the full array at the end of the loop.
Example 3. Searching for an Element in an Array, Generally
[comp1110-16] - Liam O'Connor, Fabian Muehlboeck
Example 3. Searching for an Element in an Array, Generally
[comp1110-16]- Liam O'Connor, Fabian Muehlboeck
Construction 4. import declarations
[comp1110-75] - Fabian Muehlboeck
Construction 4. import declarations
[comp1110-75]- Fabian Muehlboeck
Writing a program that does something new and amazing is great! But even in that best-case scenario, you will usually need your program to execute many little steps that are very common.
The most common of those are part of the core of your programming language, like numbers, Strings, and basic arithmetic operations. Other common things are often contained in a Standard Library, usually part of a standard language distribution, as in the case of the Java Development Kit (JDK) which you installed as part of our standard software setup. Finally, other people and organizations may publish their own libraries of code that you may be able to include in your own program.
Java's import declaration lets you "import" a library (or parts of it) so you can use its functionality in your code.
You typically put them at the top of any code file that uses them.
For example, to import the array-related part of the java.util library, you would write:
You can also import everything in a particular library using * as the last part of your imported library path, as in:
This may look convenient, but can easily cause name clashes and other maintenance issues, and as such is usually not a good idea.
Note 5. Array Equality
[comp1110-73] - Fabian Muehlboeck
Note 5. Array Equality
[comp1110-73]- Fabian Muehlboeck
We've seen earlier that String Equality requires special attention compared to the more common equality operator ==.
This is because the operator == expresses sameness, either of a numerical value, or of something larger within computer memory.
Sameness implies equality, but not necessarily the other way around.
We will look at the technical reasons for this later in the course.
Similar to Strings, Arrays are also larger things in computer memory. But in contrast to Strings, the contents of arrays can change. What that means is that if your idea of equality is that two arrays contain equal values in the same order, then that may be true at one point in your program, and false at a different point, as the contents of one or both of the arrays may change in the meantime. This means that the identity of each array matters a lot more than it does for Strings.
For that reason, a.equals(b) on arrays is the same as a == b.
However, the Java Standard Libraries contain a number of standard functions dealing with arrays, among them a suitable equality function.
To use it, you need to add an import declaration for java.util.Arrays to the top of your file.
You can then use the expression Arrays.equals(a,b) to compare two arrays for equality based on the equality of their elements.
Example 6. Arrays.equals
[comp1110-74] - Fabian Muehlboeck
Example 6. Arrays.equals
[comp1110-74]- Fabian Muehlboeck
Construction 7. Command Line Parameters
[comp1110-1L] - Liam O'Connor, Fabian Muehlboeck
Construction 7. Command Line Parameters
[comp1110-1L]- Liam O'Connor, Fabian Muehlboeck
When we invoke our program, we can pass it command line arguments:
If we add a parameter to our main function that has type String[], that array will contain each of those arguments:
When called as above, the above program will print the arguments in order, each on one line: