Section 9.13 Chapter Summary
Subsection 9.13.1 Technical Terms
array initializer | array length |
binary search | data structure |
element | element type |
insertion sort | multidimensional array |
one-dimensional array | polymorphic sort method |
selection sort | sequential search |
sorting | subscript |
two-dimensional array |
Subsection 9.13.2 Important Points
- An array is a named collection of contiguous storage locations, each of which stores a data item of the same data type. Each element of an array is referred to by a subscript —that is, by its position in the array. If the array contains N elements, then its length is N and its indexes are
0, 1, N-1
. - Array elements are referred to using the following subscript notation arrayname[subscript], where arrayname is any valid identifier, and subscript is an integer value in the range 0 to
arrayname.length - 1
. The array’slength
instance variable can be used as a bound for loops that process the array. - An array declaration provides the name and type of the array. An array instantiation uses the keyword
new
and causes the compiler to allocate memory for the array’s elements:int arr[]; // Declare a one-dimensional array variable arr = new int[15];// Allocate 15 int locations for it
- Multidimensional arrays have arrays as their components:
int twoDarr[][]; // Declare a two-dimensional array variable twoDarr = new int[10][15]; // Allocate 150 int locations
- An array’s values must be initialized by assigning values to each array location. An initializer expression may be included as part of the array declaration.
- Insertion sort and selection sort are examples of array sorting algorithms. Both algorithms require several passes over the array.
- When an array is passed as a argument to a method, a reference to the array is passed rather than the entire array itself.
- Swapping two elements of an array, or any two locations in memory, requires the use of a temporary variable.
- Sequential search and binary search are examples of array searching algorithms. Binary search requires that the array be sorted.
- For multidimensional arrays, each dimension of the array has its own
length
variable. - Inheritance and polymorphism are useful design features for developing a hierarchy of computer games.
Solutions 9.13.3 Solutions to Self-Study Exercises
9.2 One-Dimensional Arrays
9.2.3 Array Allocation
Self-Study Exercises
9.2.3.2. How many bytes double array?
9.2.3.3. How many bytes char array?
9.2.3.4. How many bytes String array?
9.2.3.5. How many bytes Student array?
9.2.5 Assigning and Using Array Values
Self-Study Exercises
9.2.5.1. What’s the value?
9.2.5.2. What’s the value?
9.2.5.3. Access the last element.
9.2.5.4. Assign to the first element.
9.2.5.5.
9.3 Simple Array Examples
Self-Study Exercise
9.3.1. Square Roots.
9.4 Example: Counting Letter Frequencies
9.4.2 A Class to Count Letter Frequencies
Self-Study Exercises
9.4.2.1. Letter Frequencies.
9.5 Array Algorithms: Sorting
9.5.1 Insertion Sort
Self Study Exercise
9.5.1.1. Run Insertion Sort.
9.5.1.2. Insertion sort trace 1.
9.5.1.3. Insertion sort trace 2.
9.5.2 Selection Sort
Self-Study Exercises
9.5.2.1. Selection sort trace 1.
9.5.2.2. Selection sort trace 2.
9.5.3 Algorithm: Swapping Memory Elements
Self-Study Exercises
9.5.3.1. Swapping values in variables.
9.5.4 Passing a Value and Passing a Reference
Self-Study Exercise
9.5.4.1. Array Parameter.
9.5.4.2. What’s the value?
9.5.4.3. Selection sort.
9.6 Array Algorithms: Searching
9.6.2 Binary Search
Self-Study Exercise
9.6.2.1. Binary search.
9.7 Two-Dimensional Arrays
9.7.1 Example: Rainfall Array
Self-Study Exercises
9.7.1.1. Two-D array.
9.7.3 Passing Part of an Array to a Method
Self-Study Exercises
9.7.3.1. Newspaper sales.
9.9 OBJECT-ORIENTED DESIGN: Polymorphic Sorting (Optional)
9.9.3 The java.util.Arrays.sort()
Method
Self-Study Exercises
9.9.3.1. Polymorphic Sort.
You have attempted of activities on this page.