For int[] a = new int[8];, a.length is:
Single-Dimensional Arrays
Declaring, creating and initializing arrays, length, default values, indexes, common processing patterns (max, shuffle, shift), the for-each loop, copying, passing and returning arrays (and why arrays behave like pass-by-reference), variable-length arguments, linear search and Arrays.sort.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 83 slides
- 1 Single-Dimensional Arrays
- 2 Opening Problem
- 3 Objectives
- 4 Introducing Arrays
- 5 Declaring Array Variables
- 6 Creating Arrays
- 7 Declaring and Creating in One Step
- 8 The Length of an Array
- 9 Default Values
- 10 Indexed Variables
- 11 Using Indexed Variables
- 12 Array Initializers
- 13 Declaring, creating, initializing Using the Shorthand Notation
- 14 CAUTION
- 15 animation
- 16 animation
- 17 animation
- 18 animation
- 19 animation
- 20 animation
- 21 animation
- 22 animation
- 23 animation
- 24 animation
- 25 animation
- 26 animation
- 27 animation
- 28 animation
- 29 animation
- 30 animation
- 31 Processing Arrays
- 32 Initializing arrays with input values
- 33 Initializing arrays with random values
- 34 Finding the largest element
- 35 Random shuffling
- 36 Shifting Elements
- 37 Enhanced for Loop (for-each loop)
- 38 Analyze Numbers
- 39 Copying Arrays
- 40 Copying Arrays
- 41 The arraycopy Utility
- 42 Passing Arrays to Methods
- 43 Anonymous Array
- 44 Pass By Value
- 45 Simple Example to pass by value and by reference
- 46 Call Stack
- 47 Call Stack
- 48 Heap
- 49 Passing Arrays as Arguments
- 50 Example, cont.
- 51 Returning an Array from a Method
- 52 Returning an Array from a Method
- 53 animation
- 54 animation
- 55 animation
- 56 animation
- 57 animation
- 58 animation
- 59 animation
- 60 animation
- 61 animation
- 62 animation
- 63 animation
- 64 animation
- 65 animation
- 66 animation
- 67 animation
- 68 animation
- 69 animation
- 70 animation
- 71 animation
- 72 animation
- 73 animation
- 74 animation
- 75 Variable-Length Arguments
- 76 Searching Arrays
- 77 Linear Searching Complete Program
- 78 Linear Search
- 79 animation
- 80 animation
- 81 Sorting Arrays
- 82 The Arrays.sort Method
- 83 The Arrays.toString(list) Method
Opening Problem
Notes
Read 100 numbers, find the average, count how many are above it → need an array.
Objectives
Notes
Objectives.
Introducing Arrays
Notes
An array is a fixed-size collection of elements of the same type.
The Length of an Array
Notes
Trap Arrays use .length; Strings use .length().
Exam angle
Default Values
Notes
Default values: 0 for numbers, \u0000 for char, false for boolean, null for references.
Exam angle
The default value of an element in new boolean[5] is:
Indexed Variables
Notes
Indexes run from 0 to length − 1; an index out of range throws ArrayIndexOutOfBoundsException.
Exam angle
Accessing a[a.length] throws:
Declaring, creating, initializing Using the Shorthand Notation
Notes
The shorthand is the same as declaring, creating and assigning each element.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Processing Arrays
Notes
Processing patterns: fill, print, sum, max, shuffle, shift.
Finding the largest element
Notes
Find the largest element (and its index).
Exam angle
Write code to find the largest value in double[] list.
Compare with the model answer
double max = list[0]; for (int i = 1; i < list.length; i++) if (list[i] > max) max = list[i];
Shifting Elements
Notes
Shift elements left by one (the first moves to the end).
Enhanced for Loop (for-each loop)
Exam angle
A for-each loop can be used to change which element each index refers to.
- Use an indexed loop to modify elements by index.
Copying Arrays
Notes
list2 = list1 copies the REFERENCE: both names point to the same array.
Trap = does not copy an array’s contents.
Exam angle
After int[] b = a; b[0] = 99;, what is a[0]?
- a and b refer to the same array.
Copying Arrays
Notes
Copy contents: a loop, System.arraycopy, or clone().
The arraycopy Utility
Notes
System.arraycopy(src, srcPos, dest, destPos, length).
Pass By Value
Exam angle
Why can a method change the elements of an array passed to it, but not an int passed to it?
Compare with the model answer
Both are passed by value, but for an array the value is a reference to the same array object on the heap; changes to its elements are visible to the caller. For an int the method only gets a copy of the value.
Simple Example to pass by value and by reference
Notes
Example: m(x, y) changes y[0] but not x.
Call Stack
Notes
Call stack holds the reference; the array itself lives on the heap.
Passing Arrays as Arguments
Notes
Swapping elements inside the array works; swapping two array references inside a method doesn’t affect the caller.
Example, cont.
Notes
Example continued.
Returning an Array from a Method
Notes
reverse implementation.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Variable-Length Arguments
Notes
Variable-length arguments: double... numbers is treated as an array; must be the last parameter.
Exam angle
Which method header uses variable-length arguments correctly?
Linear Search
Exam angle
In the worst case, linear search on n elements makes how many comparisons?
animation
Notes
Trace.
animation
Notes
Trace.