Skip to main content
Contents
Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 12.2 Writing-Arrays-WE1-P1
Subgoals for Evaluating Arrays.
Declaring an array variable
Determine data type to be stored
Data type followed by [] followed by name
Instantiating an array object
Determine size (number of elements to be stored)
Assign to variable: keyword new, followed by datatype, followed by [size]. (All values initialized to default value for datatype.)
Assign to variable: {initializer list} where initializer list contains the values to be stored in the array
Determine value of index for element to be accessed
arrayName[index]
returns value stored at that index
index must be between 0 and
(arrayName.length-1)
, inclusive otherwise an IndexOutOfBounds exception occurs at runtime
Changing value of an array element
Determine value of index of element to be changed (remember rules for index values)
Determine the expression for RHS (remember the assignment subgoals for verifying data types)
Write assignment statement to update array element
Decide if updating, accessing all in forward succession, or accessing some subset of array elements or accessing in a different order
If accessing only, write an enhanced for (for each) loop:
for (DataType varName : collectionName) - traverses collectionName from first element to last element storing a copy of each element from collectionName in varName for each iteration of the loop.
If updating or not accessing all elements in forward succession, write a for loop:
start loop control variable (which will also be index) at 0 to go forwards, (arrayName.length - 1) to go backwards
continuation test is loop control variable < arrayName.length for forwards, loop control variable >= 0 for backwards
update is loop control variable increments for forwards, decrements for backwards
inside loop access / update array element using loop control variable as index into array or varName has value of array element
Passing an array as an argument
Determine that the an entire array must be passed as an argument to a method by consulting documentation
When calling a method, put variable name that represents the array as an argument in the method call. (Remember that when passing an array as an argument that changes made by the method to the array are persistent.)
Determine that the reference to an array needs to be changed
The LHS of the assignment is the array reference needing to be changed
The RHS of the assignment is the new array reference
Subsection 12.2.1
Exercises Exercises
1.
Q1: Put the code in the appropriate order so that an array of integers will store multiples of 5 (0 to 100) in reverse order.
public int[] storeReverse() {
---
int [] reverseFives;
---
reverseFives = new int[20];
---
int multiplier = 1;
---
for (int index = reverseFives.length-1; index > 0; index-- ) {
---
reverseFives[index] = index * 5;
---
multiplier++;
---
}
---
return reverseFives;
---
}
2.
Q2. Which of the following would it be easier to use an initializer list for initialization versus a for loop? (Select all that are appropriate)
Initialize an array of author names called FavoriteAuthors
Initialize an array of numbers from 100 to 1000 called NineHundred
Initialize an array of ribbon colors called RibbonColors
Initialize an array of the top ten fastest times for your schoolβs track team in the 100 meter race named TopTen
Initialize an array with the height (in inches) of all the children in a classroom called ClassroomHeights
3.
Q3. Suppose you are writing a method that adds one to all of the elements of an array named numbers, which of the following pieces of code would be best for this task?
public void addOne(int[] numbers) {
for (int index = 0; index < numbers.length; index++ ) {
numbers[index] = numbers + 1;
}
}
public void addOne(int[] numbers) {
for (int num: numbers) {
num++;
}
}
public void addOne(int[] numbers) {
for (int num: numbers) {
numbers[num] = numbers[num] + 1;
}
}
public void addOne(int[] numbers) {
for (int index = 0; index $lt; numbers.length; index++) {
numbers[index] = index + 1;
}
}
You have attempted
of
activities on this page.