- Read 100 numbers, find the average, count how many are above it → need an array.
- Objectives.
- An array is a fixed-size collection of elements of the same type.
- Declare: double[] myList; (preferred) or double myList[];
- Create: myList = new double[10];
- One step: double[] myList = new double[10];
- Size is fixed once created: myList.length (a property, no parentheses).
🎯 How this can come up · 1 question
For int[] a = new int[8];, a.length is:
Correct: 8.
- Default values: 0 for numbers, \u0000 for char, false for boolean, null for references.
🎯 How this can come up · 1 question
The default value of an element in new boolean[5] is:
Correct: false.
- Indexes run from 0 to length − 1; an index out of range throws ArrayIndexOutOfBoundsException.
🎯 How this can come up · 1 question
Accessing a[a.length] throws:
Correct: ArrayIndexOutOfBoundsException.
- Indexed variables are used like normal variables: myList[2] = myList[0] + myList[1];
- Initializer: double[] myList = {1.9, 2.9, 3.4, 3.5};
- The shorthand is the same as declaring, creating and assigning each element.
- Caution: the initializer must be in the declaration statement.
- Trace of an array loop.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Processing patterns: fill, print, sum, max, shuffle, shift.
- Initialize from input in a loop.
- Initialize with random values.
- Find the largest element (and its index).
🎯 How this can come up · 1 question
Write code to find the largest value in double[] list.
double max = list[0]; for (int i = 1; i < list.length; i++) if (list[i] > max) max = list[i];
- Random shuffling: swap each element with a random index.
- Shift elements left by one (the first moves to the end).
- for-each: for (double e : myList) — reads every element without an index.
🎯 How this can come up · 1 question
A for-each loop can be used to change which element each index refers to.
False. Use an indexed loop to modify elements by index.
- Analyze numbers: average and count above average.
- list2 = list1 copies the REFERENCE: both names point to the same array.
🎯 How this can come up · 1 question
After int[] b = a; b[0] = 99;, what is a[0]?
a and b refer to the same array.
- Copy contents: a loop, System.arraycopy, or clone().
- System.arraycopy(src, srcPos, dest, destPos, length).
- Pass an array to a method: printArray(new int[]{3, 1, 2});
- Anonymous array: new int[]{3, 1, 2} with no variable.
- 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
Why can a method change the elements of an array passed to it, but not an int passed to it?
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.
- Example: m(x, y) changes y[0] but not x.
- Call stack holds the reference; the array itself lives on the heap.
- Call stack, continued.
- Arrays are objects stored on the heap.
- Swapping elements inside the array works; swapping two array references inside a method doesn’t affect the caller.
- Example continued.
- A method can return an array: int[] reverse(int[] list).
- reverse implementation.
- Trace of reverse.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Variable-length arguments: double... numbers is treated as an array; must be the last parameter.
🎯 How this can come up · 1 question
Which method header uses variable-length arguments correctly?
Correct: void print(String s, int... nums).
- Searching: linear search and binary search.
- Linear search compares the key with each element; returns the index or −1.
- Linear search code.
🎯 How this can come up · 1 question
In the worst case, linear search on n elements makes how many comparisons?
Correct: n.
- Trace.
- Trace.
- Sorting arrays.
- java.util.Arrays.sort(list) sorts in place.
- Arrays.toString(list) returns "[1, 2, 3]".
No slides match this filter.