



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Recall – Length of an Array Once an array has been created in memory, its size is fixed for the duration of its existence. Every array object has a length field whose value can be obtained by appending .length to the array identifier. int len = price.length; Since all arrays start numbering at zero, the length is always one greater than the highest index. Traversing Arrays To access each element in an array, a counted, or for loop, is frequently used. Suppose we have an array of 100 boolean values, automatically initialized to false by default. To change all of these to true, we could write boolean[] flags = new boolean[100]; for (int i = 0; i < flags.length; i++) flags[i] = true; Note that we have used the flags.length to determine the upper bound of the loop. As we previously noted, the .length field will return a value one greater than the maximum index of the array, which is why our loop must use i < flags.length to avoid running past the end of the array. If we attempt to use an index that is outside of the range of the array, Java will throw an exception. Comparing Arrays Arrays are objects, and as such, they have the same limitations we experience with other objects. One of those involves the notion of equality. The array variable is a reference to an array object. In other words, it points to a location in memory which holds the array and its data. We must keep this in mind when using the assignment statement (=) and the equality operator (==). For example, suppose we have made the following declaration: int[] p = {1, 2, 3, 4, 5}; int[] q = {1, 2, 3, 4, 5}; Though common sense , it would be reasonable to say they arrays are equal. They contain the same number and type of elements, and each corresponding element contains the same value. Nonetheless, if we were to test their equality, System.out.println(p == q); // false The result would be false. The reason is that the variables p and q are references to memory locations, or addresses. Since each array is distinct in memory, it must have a unique address, so they are definitely not equal. p
q
Note that Java contains a method, Arrays.equals, that compares the equality of the content of arrays. Similarly, using the assignment operator (=) could produce an unexpected situation. p = q; q[0] = 100; System.out.println(p[0]); // outputs 100 System.out.println(p == q); // now true In this case, the variables p and q now point to the same memory location, and the data contained in the original p array is forever lost. Arrays as Method Parameters Like other objects, arrays can be parameters of methods, and can be returned by methods. Since the array variable is a reference , the method will make a copy of the reference, so the original cannot be changed by the method. The referenced data , however, can and will be changed by the method unless you keep this in mind (and sometimes you want to change array data using a method, so this is desirable). public static double[] join (double[] a, double[] b) { double[] result = new double [a.length + b.length]; int i, j; for (i = 0; i < a.length; i++) result[i] = a[i]; for (j = 0; j < b.length; i++, j++) result[i] = b[j]; return result; } Note that when introducing arrays, we discussed that their size was fixed. While this is true, it is possible to dynamically create a new array of any size, and if necessary, abandon the original array (which, in effect, allows a programmer to dynamically alter the size of an array). p
q
Exercises
Solutions
for (int i = 0; i < sample.length; i ++) sample[i] = 1;
int temp = sample[0]; sample[0] = sample[sample.length – 1]; sample[sample.length – 1] = temp;
for (int i = 0; i < sample.length; i ++) sample[i] = (int)Math.abs(sample[i]);
int sampleSum = 0; for (int i = 0; i < sample.length; i ++) sampleSum += sample[i];
for (int i = 0; i < sample.length; i ++) { if (i % 2 != 0) System.out.println(sample[i]); }