CS101 · Annotated SlidesChapter 7 — Single-Dimensional Arrays
Chapter 7 · Annotated slides

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.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up

Single-Dimensional Arrays

CS101 · Chapter 7
1
Slide 2: Opening Problem
Exam weight Low
  1. Read 100 numbers, find the average, count how many are above it → need an array.
Slide 3: Objectives
Exam weight Low
  1. Objectives.
Slide 4: Introducing Arrays
Exam weight Low
  1. An array is a fixed-size collection of elements of the same type.
Slide 5: Declaring Array Variables
Exam weight Low
  1. Declare: double[] myList; (preferred) or double myList[];
Slide 6: Creating Arrays
Exam weight Low
  1. Create: myList = new double[10];
Slide 7: Declaring and Creating in One Step
Exam weight Low
  1. One step: double[] myList = new double[10];
Slide 8: The Length of an Array
Exam weight High
  1. Size is fixed once created: myList.length (a property, no parentheses).
Exam trapArrays use .length; Strings use .length().
🎯 How this can come up · 1 question
MCQ

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

Slide 9: Default Values
Exam weight High
  1. Default values: 0 for numbers, \u0000 for char, false for boolean, null for references.
🎯 How this can come up · 1 question
MCQ

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

Slide 10: Indexed Variables
Exam weight High
  1. Indexes run from 0 to length − 1; an index out of range throws ArrayIndexOutOfBoundsException.
🎯 How this can come up · 1 question
MCQ

Accessing a[a.length] throws:

Slide 11: Using Indexed Variables
Exam weight Low
  1. Indexed variables are used like normal variables: myList[2] = myList[0] + myList[1];
Slide 12: Array Initializers
Exam weight Low
  1. Initializer: double[] myList = {1.9, 2.9, 3.4, 3.5};
Slide 13: Declaring, creating, initializing Using the Shorthand Notation
Exam weight Low
  1. The shorthand is the same as declaring, creating and assigning each element.
Slide 14: CAUTION
Exam weight Low
  1. Caution: the initializer must be in the declaration statement.
Slide 15: animation
Exam weight Low
  1. Trace of an array loop.
Slide 16: animation
Exam weight Low
  1. Trace.
Slide 17: animation
Exam weight Low
  1. Trace.
Slide 18: animation
Exam weight Low
  1. Trace.
Slide 19: animation
Exam weight Low
  1. Trace.
Slide 20: animation
Exam weight Low
  1. Trace.
Slide 21: animation
Exam weight Low
  1. Trace.
Slide 22: animation
Exam weight Low
  1. Trace.
Slide 23: animation
Exam weight Low
  1. Trace.
Slide 24: animation
Exam weight Low
  1. Trace.
Slide 25: animation
Exam weight Low
  1. Trace.
Slide 26: animation
Exam weight Low
  1. Trace.
Slide 27: animation
Exam weight Low
  1. Trace.
Slide 28: animation
Exam weight Low
  1. Trace.
Slide 29: animation
Exam weight Low
  1. Trace.
Slide 30: animation
Exam weight Low
  1. Trace.
Slide 31: Processing Arrays
Exam weight Low
  1. Processing patterns: fill, print, sum, max, shuffle, shift.
Slide 32: Initializing arrays with input values
Exam weight Low
  1. Initialize from input in a loop.
Slide 33: Initializing arrays with random values
Exam weight Low
  1. Initialize with random values.
Slide 34: Finding the largest element
Exam weight High
  1. Find the largest element (and its index).
🎯 How this can come up · 1 question
Written

Write code to find the largest value in double[] list.

Slide 35: Random shuffling
Exam weight Low
  1. Random shuffling: swap each element with a random index.
Slide 36: Shifting Elements
Exam weight Low
  1. Shift elements left by one (the first moves to the end).
Slide 37: Enhanced for Loop (for-each loop)
Exam weight Medium
  1. for-each: for (double e : myList) — reads every element without an index.
🎯 How this can come up · 1 question
True / False

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

Slide 38: Analyze Numbers
Exam weight Low
  1. Analyze numbers: average and count above average.
Slide 39: Copying Arrays
Exam weight High
  1. list2 = list1 copies the REFERENCE: both names point to the same array.
Exam trap= does not copy an array’s contents.
🎯 How this can come up · 1 question
MCQ

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

Slide 40: Copying Arrays
Exam weight Low
  1. Copy contents: a loop, System.arraycopy, or clone().
Slide 41: The arraycopy Utility
Exam weight Low
  1. System.arraycopy(src, srcPos, dest, destPos, length).
Slide 42: Passing Arrays to Methods
Exam weight Low
  1. Pass an array to a method: printArray(new int[]{3, 1, 2});
Slide 43: Anonymous Array
Exam weight Low
  1. Anonymous array: new int[]{3, 1, 2} with no variable.
Slide 44: Pass By Value
Exam weight High
  1. Java is pass-by-value; for an array the value passed is the REFERENCE, so the method can change the caller’s elements.
🎯 How this can come up · 1 question
Written

Why can a method change the elements of an array passed to it, but not an int passed to it?

Slide 45: Simple Example to pass by value and by reference
Exam weight Low
  1. Example: m(x, y) changes y[0] but not x.
Slide 46: Call Stack
Exam weight Low
  1. Call stack holds the reference; the array itself lives on the heap.
Slide 47: Call Stack
Exam weight Low
  1. Call stack, continued.
Slide 48: Heap
Exam weight Low
  1. Arrays are objects stored on the heap.
Slide 49: Passing Arrays as Arguments
Exam weight Low
  1. Swapping elements inside the array works; swapping two array references inside a method doesn’t affect the caller.
Slide 50: Example, cont.
Exam weight Low
  1. Example continued.
Slide 51: Returning an Array from a Method
Exam weight Low
  1. A method can return an array: int[] reverse(int[] list).
Slide 52: Returning an Array from a Method
Exam weight Low
  1. reverse implementation.
Slide 53: animation
Exam weight Low
  1. Trace of reverse.
Slide 54: animation
Exam weight Low
  1. Trace.
Slide 55: animation
Exam weight Low
  1. Trace.
Slide 56: animation
Exam weight Low
  1. Trace.
Slide 57: animation
Exam weight Low
  1. Trace.
Slide 58: animation
Exam weight Low
  1. Trace.
Slide 59: animation
Exam weight Low
  1. Trace.
Slide 60: animation
Exam weight Low
  1. Trace.
Slide 61: animation
Exam weight Low
  1. Trace.
Slide 62: animation
Exam weight Low
  1. Trace.
Slide 63: animation
Exam weight Low
  1. Trace.
Slide 64: animation
Exam weight Low
  1. Trace.
Slide 65: animation
Exam weight Low
  1. Trace.
Slide 66: animation
Exam weight Low
  1. Trace.
Slide 67: animation
Exam weight Low
  1. Trace.
Slide 68: animation
Exam weight Low
  1. Trace.
Slide 69: animation
Exam weight Low
  1. Trace.
Slide 70: animation
Exam weight Low
  1. Trace.
Slide 71: animation
Exam weight Low
  1. Trace.
Slide 72: animation
Exam weight Low
  1. Trace.
Slide 73: animation
Exam weight Low
  1. Trace.
Slide 74: animation
Exam weight Low
  1. Trace.
Slide 75: Variable-Length Arguments
Exam weight Medium
  1. Variable-length arguments: double... numbers is treated as an array; must be the last parameter.
🎯 How this can come up · 1 question
MCQ

Which method header uses variable-length arguments correctly?

Slide 76: Searching Arrays
Exam weight Low
  1. Searching: linear search and binary search.
Slide 77: Linear Searching Complete Program
Exam weight Low
  1. Linear search compares the key with each element; returns the index or −1.
Slide 78: Linear Search
Exam weight High
  1. Linear search code.
🎯 How this can come up · 1 question
MCQ

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

Slide 79: animation
Exam weight Low
  1. Trace.
Slide 80: animation
Exam weight Low
  1. Trace.
Slide 81: Sorting Arrays
Exam weight Low
  1. Sorting arrays.
Slide 82: The Arrays.sort Method
Exam weight Low
  1. java.util.Arrays.sort(list) sorts in place.
Slide 83: The Arrays.toString(list) Method
Exam weight Low
  1. Arrays.toString(list) returns "[1, 2, 3]".