Arrays

Related slides on offer

Figure 197. Motivating Arrays Slide presentation
final String
      karen = "Karen Smith",
      john = "John Duncan",
      paul = "Paul Jacobs",
      suzanne = "Suzanne Enders",
      peter = "Peter Phillips";  // 10 more to come ...

    IO.println(karen);
    IO.println(john);
...

Figure 198. Per member repeating tasks Slide presentation
  • Generate Comma separated list:

    Karen Smith, John Duncan, Paul Jacobs, Suzanne Enders, Peter Phillips
  • Generate HTML list emphasizing family names:

    <ul>
      <li>Karen <emph>Smith</emph></li>
      <li>John <emph>Duncan</emph></li>
      <li>Paul <emph>Jacobs</emph></li>
      <li>Suzanne <emph>Enders</emph></li>
      <li>Peter <emph>Phillips</emph></li>
    </ul>

Figure 199. Example: int array of primes Slide presentation
final int[] primes  = new int[5]; 

primes[0] = 2; 
primes[1] = 3;
primes[2] = 5;
primes[3] = 7;
primes[4] = 11;

int array reference variable primes.

int array allocation capable to store five values and assigning its reference to variable primes.

Assigning values referencing array positions by index values from {0,1,2,3,4}.


exercise No. 93

Assignment to final variable?

Q:

In Figure 199, “Example: int array of primes ” a final variable primes is being declared. Despite the final modifier denoting constant values we see a whole bunch of assignments i.e. primes[0] = 2. Give an explanation.

  • Explain this seeming contradiction.

  • Provide a code example demonstrating the final declaration's consequence.

A:

The variable primes carries a reference to an array. The final modifier declares this reference to become non-modifiable. On contrary changing the array's values is not being restricted. Consider:

final int[] primes  = new int[5];

primes[0] = 0;        // O.k.: Changing first array value

primes = new int[3]; // Error: Cannot assign a value to final variable 'primes'
Figure 200. Loop prime values Slide presentation
for (int i = 0; i < 5; i++) {
  IO.println("At index " + i + ": value == " + primes[i]);
}

Result:

At index 0: value == 2
At index 1: value == 3
At index 2: value == 5
At index 3: value == 7
At index 4: value == 11

Figure 201. Mind the limit! Slide presentation
for (int i = 0; i < 6; i++) {
  IO.println("At index " + i + ": value == " + primes[i]);
}

Result:

At index 0: value == 2
At index 1: value == 3
At index 2: value == 5
At index 3: value == 7
At index 4: value == 11
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
  at Primes.main(Primes.java:5)

Figure 202. Safer: Using length Slide presentation
IO.println("primes.length == " + primes.length);
for (int i = 0; i < primes.length; i++) {
  IO.println("At index " + i + ": value == " + primes[i]);
}
Result:
primes.length == 5
At index 0: value == 2
At index 1: value == 3
At index 2: value == 5
At index 3: value == 7
At index 4: value == 11

Figure 203. Even better: for-each style loop Slide presentation
for (final int p: primes) {
  IO.println("value == " + p);
}
Result:
value == 2
value == 3
value == 5
value == 7
value == 11

Figure 204. Mind the limit, part two Slide presentation
final int[] primes = new int[5]; // Last index is 4 rather than 5!

primes[0] = 2;
primes[1] = 3;
primes[2] = 5;
primes[3] = 7;
primes[4] = 11;
primes[5] = 13; // Excessing array limit
Result:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
  at Primes.main(Primes.java:25)

Figure 205. Primitive data one step initialization Slide presentation

Combining array allocation and value assignment:

final int[]
  primes = {2, 3, 5, 7, 11};
final int[] primes = new int[5];
primes[0] = 2;
primes[1] = 3;
primes[2] = 5;
primes[3] = 7;
primes[4] = 11;

Figure 206. Array Slide presentation
  • Series of objects having identical type.

  • Array consists of array elements.

  • Element access by index value.

  • Holding either primitive types or object references (Class instance or array).

  • Contiguous storage in memory.

  • Arbitrary array dimensions by virtue of nesting: One-dimensional, two-dimensional etc.


Figure 207. Two syntax variants Slide presentation
  1. type[] arrayName; // preferred
  2. type arrayName[];

Figure 208. Passing an anonymous array to a method Slide presentation
void main() {
  // Error: Array initializer is not allowed here
  double average1 = getAverage({1, 5, 20, 4});

  // Works!
  double average2 = getAverage(new int[]{1, 5, 20, 4});
}

/**
 * Compute an array's average of its values
 *
 * @param values Values to be considered
 * @return The average of all values
 */
double getAverage(int[] values) {
   ...
   return ...;
}

exercise No. 94

Implementing getAverage(...).

Q:

Implement getAverage(...) from Figure 208, “Passing an anonymous array to a method ”.