CS101 · Chapter 7 · Annotated SlidesSingle-Dimensional Arrays
Original
Overview

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. 1 Single-Dimensional Arrays
  2. 2 Opening Problem
  3. 3 Objectives
  4. 4 Introducing Arrays
  5. 5 Declaring Array Variables
  6. 6 Creating Arrays
  7. 7 Declaring and Creating in One Step
  8. 8 The Length of an Array
  9. 9 Default Values
  10. 10 Indexed Variables
  11. 11 Using Indexed Variables
  12. 12 Array Initializers
  13. 13 Declaring, creating, initializing Using the Shorthand Notation
  14. 14 CAUTION
  15. 15 animation
  16. 16 animation
  17. 17 animation
  18. 18 animation
  19. 19 animation
  20. 20 animation
  21. 21 animation
  22. 22 animation
  23. 23 animation
  24. 24 animation
  25. 25 animation
  26. 26 animation
  27. 27 animation
  28. 28 animation
  29. 29 animation
  30. 30 animation
  31. 31 Processing Arrays
  32. 32 Initializing arrays with input values
  33. 33 Initializing arrays with random values
  34. 34 Finding the largest element
  35. 35 Random shuffling
  36. 36 Shifting Elements
  37. 37 Enhanced for Loop (for-each loop)
  38. 38 Analyze Numbers
  39. 39 Copying Arrays
  40. 40 Copying Arrays
  41. 41 The arraycopy Utility
  42. 42 Passing Arrays to Methods
  43. 43 Anonymous Array
  44. 44 Pass By Value
  45. 45 Simple Example to pass by value and by reference
  46. 46 Call Stack
  47. 47 Call Stack
  48. 48 Heap
  49. 49 Passing Arrays as Arguments
  50. 50 Example, cont.
  51. 51 Returning an Array from a Method
  52. 52 Returning an Array from a Method
  53. 53 animation
  54. 54 animation
  55. 55 animation
  56. 56 animation
  57. 57 animation
  58. 58 animation
  59. 59 animation
  60. 60 animation
  61. 61 animation
  62. 62 animation
  63. 63 animation
  64. 64 animation
  65. 65 animation
  66. 66 animation
  67. 67 animation
  68. 68 animation
  69. 69 animation
  70. 70 animation
  71. 71 animation
  72. 72 animation
  73. 73 animation
  74. 74 animation
  75. 75 Variable-Length Arguments
  76. 76 Searching Arrays
  77. 77 Linear Searching Complete Program
  78. 78 Linear Search
  79. 79 animation
  80. 80 animation
  81. 81 Sorting Arrays
  82. 82 The Arrays.sort Method
  83. 83 The Arrays.toString(list) Method
Slide 1 / 83

Single-Dimensional Arrays

Slide 1: Single-Dimensional Arrays
Slide 2 / 83

Opening Problem

Slide 2: Opening Problem

Notes

  1. Read 100 numbers, find the average, count how many are above it → need an array.

Slide 3 / 83

Objectives

Slide 3: Objectives

Notes

  1. Objectives.

Slide 4 / 83

Introducing Arrays

Slide 4: Introducing Arrays

Notes

  1. An array is a fixed-size collection of elements of the same type.

Slide 5 / 83

Declaring Array Variables

Slide 5: Declaring Array Variables
Slide 6 / 83

Creating Arrays

Slide 6: Creating Arrays
Slide 7 / 83

Declaring and Creating in One Step

Slide 7: Declaring and Creating in One Step
Slide 8 / 83

The Length of an Array

Slide 8: The Length of an Array

Notes

  1. Trap Arrays use .length; Strings use .length().

Exam angle

PredictionQ1

For int[] a = new int[8];, a.length is:

Slide 9 / 83

Default Values

Slide 9: Default Values

Notes

  1. Default values: 0 for numbers, \u0000 for char, false for boolean, null for references.

Exam angle

PredictionQ1

The default value of an element in new boolean[5] is:

Slide 10 / 83

Indexed Variables

Slide 10: Indexed Variables

Notes

  1. Indexes run from 0 to length − 1; an index out of range throws ArrayIndexOutOfBoundsException.

Exam angle

PredictionQ1

Accessing a[a.length] throws:

Slide 11 / 83

Using Indexed Variables

Slide 11: Using Indexed Variables
Slide 12 / 83

Array Initializers

Slide 12: Array Initializers
Slide 13 / 83

Declaring, creating, initializing Using the Shorthand Notation

Slide 13: Declaring, creating, initializing Using the Shorthand Notation

Notes

  1. The shorthand is the same as declaring, creating and assigning each element.

Slide 14 / 83

CAUTION

Slide 14: CAUTION
Slide 15 / 83

animation

Slide 15: animation
Slide 16 / 83

animation

Slide 16: animation

Notes

  1. Trace.

Slide 17 / 83

animation

Slide 17: animation

Notes

  1. Trace.

Slide 18 / 83

animation

Slide 18: animation

Notes

  1. Trace.

Slide 19 / 83

animation

Slide 19: animation

Notes

  1. Trace.

Slide 20 / 83

animation

Slide 20: animation

Notes

  1. Trace.

Slide 21 / 83

animation

Slide 21: animation

Notes

  1. Trace.

Slide 22 / 83

animation

Slide 22: animation

Notes

  1. Trace.

Slide 23 / 83

animation

Slide 23: animation

Notes

  1. Trace.

Slide 24 / 83

animation

Slide 24: animation

Notes

  1. Trace.

Slide 25 / 83

animation

Slide 25: animation

Notes

  1. Trace.

Slide 26 / 83

animation

Slide 26: animation

Notes

  1. Trace.

Slide 27 / 83

animation

Slide 27: animation

Notes

  1. Trace.

Slide 28 / 83

animation

Slide 28: animation

Notes

  1. Trace.

Slide 29 / 83

animation

Slide 29: animation

Notes

  1. Trace.

Slide 30 / 83

animation

Slide 30: animation

Notes

  1. Trace.

Slide 31 / 83

Processing Arrays

Slide 31: Processing Arrays

Notes

  1. Processing patterns: fill, print, sum, max, shuffle, shift.

Slide 32 / 83

Initializing arrays with input values

Slide 32: Initializing arrays with input values
Slide 33 / 83

Initializing arrays with random values

Slide 33: Initializing arrays with random values
Slide 34 / 83

Finding the largest element

Slide 34: Finding the largest element

Notes

  1. Find the largest element (and its index).

Exam angle

PredictionQ1

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];

Slide 35 / 83

Random shuffling

Slide 35: Random shuffling
Slide 36 / 83

Shifting Elements

Slide 36: Shifting Elements

Notes

  1. Shift elements left by one (the first moves to the end).

Slide 37 / 83

Enhanced for Loop (for-each loop)

Slide 37: Enhanced for Loop (for-each loop)

Exam angle

PredictionQ1

A for-each loop can be used to change which element each index refers to.

Slide 38 / 83

Analyze Numbers

Slide 38: Analyze Numbers
Slide 39 / 83

Copying Arrays

Slide 39: Copying Arrays

Notes

  1. list2 = list1 copies the REFERENCE: both names point to the same array.

  2. Trap = does not copy an array’s contents.

Exam angle

PredictionQ1

After int[] b = a; b[0] = 99;, what is a[0]?

Slide 40 / 83

Copying Arrays

Slide 40: Copying Arrays

Notes

  1. Copy contents: a loop, System.arraycopy, or clone().

Slide 41 / 83

The arraycopy Utility

Slide 41: The arraycopy Utility

Notes

  1. System.arraycopy(src, srcPos, dest, destPos, length).

Slide 42 / 83

Passing Arrays to Methods

Slide 42: Passing Arrays to Methods
Slide 43 / 83

Anonymous Array

Slide 43: Anonymous Array
Slide 44 / 83

Pass By Value

Slide 44: Pass By Value

Exam angle

PredictionQ1

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.

Slide 45 / 83

Simple Example to pass by value and by reference

Slide 45: Simple Example to pass by value and by reference

Notes

  1. Example: m(x, y) changes y[0] but not x.

Slide 46 / 83

Call Stack

Slide 46: Call Stack

Notes

  1. Call stack holds the reference; the array itself lives on the heap.

Slide 47 / 83

Call Stack

Slide 47: Call Stack
Slide 48 / 83

Heap

Slide 48: Heap
Slide 49 / 83

Passing Arrays as Arguments

Slide 49: Passing Arrays as Arguments

Notes

  1. Swapping elements inside the array works; swapping two array references inside a method doesn’t affect the caller.

Slide 50 / 83

Example, cont.

Slide 50: Example, cont.

Notes

  1. Example continued.

Slide 51 / 83

Returning an Array from a Method

Slide 51: Returning an Array from a Method
Slide 52 / 83

Returning an Array from a Method

Slide 52: Returning an Array from a Method

Notes

  1. reverse implementation.

Slide 53 / 83

animation

Slide 53: animation
Slide 54 / 83

animation

Slide 54: animation

Notes

  1. Trace.

Slide 55 / 83

animation

Slide 55: animation

Notes

  1. Trace.

Slide 56 / 83

animation

Slide 56: animation

Notes

  1. Trace.

Slide 57 / 83

animation

Slide 57: animation

Notes

  1. Trace.

Slide 58 / 83

animation

Slide 58: animation

Notes

  1. Trace.

Slide 59 / 83

animation

Slide 59: animation

Notes

  1. Trace.

Slide 60 / 83

animation

Slide 60: animation

Notes

  1. Trace.

Slide 61 / 83

animation

Slide 61: animation

Notes

  1. Trace.

Slide 62 / 83

animation

Slide 62: animation

Notes

  1. Trace.

Slide 63 / 83

animation

Slide 63: animation

Notes

  1. Trace.

Slide 64 / 83

animation

Slide 64: animation

Notes

  1. Trace.

Slide 65 / 83

animation

Slide 65: animation

Notes

  1. Trace.

Slide 66 / 83

animation

Slide 66: animation

Notes

  1. Trace.

Slide 67 / 83

animation

Slide 67: animation

Notes

  1. Trace.

Slide 68 / 83

animation

Slide 68: animation

Notes

  1. Trace.

Slide 69 / 83

animation

Slide 69: animation

Notes

  1. Trace.

Slide 70 / 83

animation

Slide 70: animation

Notes

  1. Trace.

Slide 71 / 83

animation

Slide 71: animation

Notes

  1. Trace.

Slide 72 / 83

animation

Slide 72: animation

Notes

  1. Trace.

Slide 73 / 83

animation

Slide 73: animation

Notes

  1. Trace.

Slide 74 / 83

animation

Slide 74: animation

Notes

  1. Trace.

Slide 75 / 83

Variable-Length Arguments

Slide 75: Variable-Length Arguments

Notes

  1. Variable-length arguments: double... numbers is treated as an array; must be the last parameter.

Exam angle

PredictionQ1

Which method header uses variable-length arguments correctly?

Slide 76 / 83

Searching Arrays

Slide 76: Searching Arrays
Slide 77 / 83

Linear Searching Complete Program

Slide 77: Linear Searching Complete Program
Slide 78 / 83

Linear Search

Slide 78: Linear Search

Exam angle

PredictionQ1

In the worst case, linear search on n elements makes how many comparisons?

Slide 79 / 83

animation

Slide 79: animation

Notes

  1. Trace.

Slide 80 / 83

animation

Slide 80: animation

Notes

  1. Trace.

Slide 81 / 83

Sorting Arrays

Slide 81: Sorting Arrays
Slide 82 / 83

The Arrays.sort Method

Slide 82: The Arrays.sort Method
Slide 83 / 83

The Arrays.toString(list) Method

Slide 83: The Arrays.toString(list) Method