Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
int array of primes
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
lengthLecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Two-dimensional arraysLecture notes |
Pdf slides |
|
Behind the scenesLecture notes |
Pdf slides |
|
Memory allocationLecture notes |
Pdf slides |
|
(#1 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#2 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#3 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#4 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#5 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#6 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#7 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#8 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#9 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#10 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#11 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#12 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#13 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#14 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#15 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#16 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#17 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#18 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#19 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#20 of 21) |
Memory allocationLecture notes |
Pdf slides |
|
(#21 of 21) |
Lecture notes |
Pdf slides |
|
Nested array initializationLecture notes |
Pdf slides |
|
Nested »ragged« array
initializationLecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Multiple values of common type.
Loop style handling.
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);
...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>final int[] primes ❶ = new int[5]; ❷ primes[0] = 2; ❸ primes[1] = 3; primes[2] = 5; primes[3] = 7; primes[4] = 11;
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
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)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 |
|
|
| Result: | value == 2 value == 3 value == 5 value == 7 value == 11 |
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) |
Combining array allocation and value assignment:
|
|
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.
type[] arrayName; // preferredtype arrayName[];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 ...;
}Multiple dimensions by nesting of arrays.
| Code | Result |
|---|---|
// Allocation final int[][] MATRIX = new int[2][3]; // Initialization for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { MATRIX[row][col] = col + row; } } // Output for (int row = 0; row < 2; row++) { IO.println(Arrays.toString(MATRIX[row])); } |
[0, 1, 2] [1, 2, 3] |
final int[][] MATRIX = new int[2][]; // Array containing two int arrays
MATRIX[0] = new int[3]; // first int array
MATRIX[1] = new int[3]; // second int arrayConclusions:
Java only supports one-dimensional arrays.
By means of nesting we get the illusion of multi- dimensional arrays.
final int[][] MATRIX = new int[][] {
{0, 1, 2},
{1, 2, 3}
};| Code | Result |
|---|---|
|
[Jill Tom ] [Jane Smith Joe ] [Jeff ] |