Chapter 7 Arrays (Reading)

Ace your homework & exams now with Quizwiz!

7.11 A program has the following declaration: double[] values; Write code that asks the user for the size of the array and then creates an array of the specified size, referenced by the values variable.

// Assume keyboard references a Scanner object. int size; System.out.print("Enter the size of the array: "); size = keyboard.nextInt(); values = new double[size];

Because array subscripts start at ___ rather than 1, you have to be careful not to perform an off-by-one error.

0

7.3 What would the valid subscript values be in a four-element array of doubles?

0 through 3

7.7 What is the output of the following code? int[] values = new int[5]; for (int count = 0; count < 5; count++) values[count] = count + 1; for (int count = 0; count < 5; count++) System.out.println(values[count]);

1 2 3 4 5

7.25 Write a statement that creates an ArrayList object and assigns its address to a variable named lizards. The ArrayList should be able to store String objects only.

ArrayList<String> lizards = new ArrayList<String>();

7.26 How do you add items to an ArrayList object?

You use the add method.

7.1 Write statements that create the following arrays: b) A 25-element double array referenced by the variable payRates.

double[] payRates = new double[25];

An array's size declarator must be a __ ___ integer expression.

non-negative

The ellipsis (three periods) that follows the data type indicates that numbers is a special type of parameter known as a ____ ________.

vararg parameter.

7.21 On average, with an array of 20,000 elements, how many comparisons will the sequential search perform? (Assume the items being searched for are consistently found in the array.)

10,000 comparisons

7.31 What is the difference between an ArrayList object's size and its capacity?

An ArrayList's size is the number of items stored in the ArrayList object. An ArrayList's capacity is the number of items the ArrayList object can hold without having to increase its size.

An ______object automatically expands as items are added to it.

ArrayList

An ______object automatically shrinks as items are removed from it.

ArrayList

In addition to adding items to an _____, you can remove items as well.

ArrayList

The Java API provides a class named ______, which can be used for storing and retrieving objects.

ArrayList

7.24 Write a statement that creates an ArrayList object and assigns its address to a variable named frogs.

ArrayList frogs = new ArrayList();

7.29 How do you insert an item at a specific location in an ArrayList object?

Inserting means adding an item at a specific index. The ArrayList class has an overloaded version of the add method that allows you to add an item at a specific index.

7.22 If a sequential search is performed on an array, and it is known that some items are searched for more frequently than others, how can the contents of the array be reordered to improve the average performance of the search?

Move the items that are frequently searched for to the beginning of the array.

7.19 How many times will the selection sort swap the smallest value in an array with another value?

Only once.

7.28 How do you retrieve a specific item from an ArrayList object?

The ArrayList class has a get method that retrieves an item at a specific index. You pass the index as an argument to the method.

____ ____arrays, which are sometimes called 2D arrays, can hold multiple sets of data.

Two-dimensional

7.6 What happens in Java when a program tries to use a subscript that is out -of- bounds?

When the statement executes, it crashes the program and displays a runtime error message.

to ______an array you need to copy the individual elements of one array to another.

copy

It is possible to reassign an array reference variable to a _______array

different

Processing array elements is no ______from processing other variables

different

To compare the contents of two arrays, you must compare the _________of the two arrays.

elements

The ______ for loop is designed to iterate once for every element in an array. Each time the loop iterates, it copies an array element to a variable.

enhanced

7.23 What import statement must you include in your code in order to use the ArrayList class?

import java.util.ArrayList;

The ArrayList class is in the java.util package, so the following import statement is required:

import java.util.ArrayList;

Note that you do not use the new key word when you use an _____ ______. Java automatically creates the array and stores the values in the initialization list in it.

initialization list

When _____ a two-dimensional array, you enclose each row's initialization list in its own set of braces.

initializing

7.1 Write statements that create the following arrays: a) A 100-element int array referenced by the variable employeeNumbers.

int[] employeesNumbers = new int[100];

Each array in Java has a public field named _____. This field contains the number of elements in the array.

length

Be careful not to cause an off-by-one error when using the length field as the upper limit of a subscript. The length field contains the number of elements in an array. The largest subscript in an array is ______

length - 1

Java does not limit the number of dimensions that an array may have. It is possible to create arrays with _____ dimensions, to model data that occurs in multiple sets.

multiple

7.13 Look at the following method header: public static void myMethod(double[] array) Here is an array declaration: double[] numbers = new double[100]; Write a statement that passes the numbers array to the myMethod method.

myMethod(numbers);

The subscript of the last element in an array is ___ ____ than the total number of elements in the array.

one less

They are called ____ _____because they can hold only one set of data

one-dimensional

When a two-dimensional array is passed to a method, the ____must be declared as a reference to a two-dimensional array.

parameter

The ArrayList class's ____method can be used to replace an item at a specific index with another item.

set

The ArrayList class has a ______method that reports the number of items stored in an ArrayList. It returns the number of items as an int.

size

The ArrayList class has a ____method that returns a string representing all of the items stored in an ArrayList object.

toString

A __ ____ array is an array of arrays. It can be thought of as having rows and columns.

two-dimensional

You can create an ArrayList to hold any _____of object.

type

An array can be passed as an argument to a method. To pass an array, you pass the ____in the variable that references the array.

value

Java provides a mechanism known as ____ _____ _____ _____ which makes it possible to write a method that takes a variable number of arguments.

variable-length argument lists,

Subscript numbers can be stored in ______

variables

Subscript numbering always starts at ______.

zero

Write statements that create the following arrays: a) A 100-element int array referenced by the variable employeeNumbers. b) A 25-element double array referenced by the variable payRates. c) A 14-element float array referenced by the variable miles. d) A 1000-element char array referenced by the variable letters.

a) int[] employeeNumbers = new int[100]; b) double[] payRates = new double[25]; c) float[] miles = new float[14]; d) char[] letters = new char[1000];

To add items to the ArrayList object, you use the _____method.

add

What's wrong with the following array declarations? int[] readings = new int[-1]; double[] measurements = new double[4.5];

An array's size declarator must be a non-negative integer expression. The first statement is incorrect because the size declarator is negative. The second statement is incorrect because the size declarator is a floating-point number.

7.27 How do you remove an item from an ArrayList object?

The ArrayList class has a remove method that removes an item at a specific index. You pass the index as an argument to the method.

7.30 How do you determine an ArrayList object's size?

The ArrayList class has a size method that returns the number of items stored in the ArrayList.

7.18 What value in an array does the selection sort algorithm look for first? When the selection sort finds this value, what does it do with it?

The selection sort first looks for the smallest value in the array. When it finds it, it moves it to element 0.

7.20 Describe the difference between the sequential search and the binary search.

The sequential search steps through each element of the array, starting at element 0, looking for the search value. The binary search requires that the array be sorted in ascending order. It starts by looking at the middle element. If it is not the search value, and is greater than the search value, then the lower half of the array is searched next. If the middle element is not the search value, and is less than the search value, the upper half of the array is searched next. This same technique is repeated on the half of the array being searched until the element is either found or there are no more elements to search.

7.4 What is the difference between an array's size declarator and a subscript?

The size declarator specifies the number of elements in the array. A subscript identifies a specific element in the array.

7.5 What does it mean for a subscript to be out-of-bounds?

The subscript is outside the range of valid subscripts for the array.

The ArrayList class has an overloaded version of the ____method that allows you to add an item at a specific index.

add

When an item is removed from an ArrayList, the items that come _____it are shifted downward in position to fill the empty space. This means that the index of each item after the removed item will be decreased by one.

after

A sorting ____ is a technique for scanning through an array and rearranging its contents in some specific order. T

algorithm

An _____can hold multiple values of the same data type simultaneously.

array

The ____search is a clever algorithm that is much more efficient than the sequential search.

binary

Java performs array _____ ______, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.

bounds checking

Java allows you to use two different styles when declaring array reference variables. The _____immediately following the data type. And the _____are placed after the variable name

brackets, brackets

You ____ change the value of an array's length field.

cannot

An ArrayList object also has a ___, which is the number of items it can store without having to increase its size.

capacity

Once an array is created, its size cannot be _______

changed.

7.1 Write statements that create the following arrays: A 1000-element char array referenced by the variable letters.

char[] letters = new char[1000];

7.8 Write a statement that creates and initializes a double array with the following values: 1.7, 6.4, 8.9, 3.1, and 9.2. How many elements are in the array?

double[] array = { 1.7, 6.4, 8.9, 3.1, 9.2 }; There are five elements in the array.

A one-dimensional array has a length field that holds the number of ______in the array.

elements

When an _____array is passed into a method, it is passed just as an object is passed: The actual array itself is not passed, but a reference to the array is passed into the parameter. Consequently, this means the method has direct access to the original array.

entire

The Java compiler does not display an error message when it processes a statement that uses an invalid subscript. Instead, when the statement executes, the program throws an ______ and immediately terminates.

exception

It is a common practice to use a __ _____ as a size declarator.

final variable

7.1 Write statements that create the following arrays: c) A 14-element float array referenced by the variable miles.

float[] miles = new float[14];

7.12 Look at the following statements: int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = new int[7]; Write code that copies the a array to the b array.

for (int i = 0; i < a.length; i++) b[i] = a[i];

7.10 A program uses a variable named array that references an array of integers. You do not know the number of elements in the array. Write a for loop that stores −1 in each element of the array.

for (int i = 0; i < array.length; i++) array[i] = -1;

The ArrayList class's _____method returns the item stored at a specific index.

get

Declaring an array reference variable does not create an array. use the ______key word to create an array and assign its address to the variable

new

7.14 Write a method named zero, which accepts an int array as an argument and stores the value 0 in each element.

public static void zero(int[] array) { for (int i = 0; i < array.length; i++) array[i] = 0; }

When the rows of a two-dimensional array are of different lengths, the array is known as a ______array.

ragged

You create a _____ array by first creating a two-dimensional array with a specific number of rows, but no columns.

ragged

7.9 Look at the following statements: int[] numbers1 = { 1, 3, 6, 9 }; int[] numbers2 = { 2, 4, 6, 8 }; int result; Write a statement that multiplies element 0 of the numbers1 array by element 3 of the numbers2 array and assigns the result to the result variable.

result = numbers1[0] * numbers2[3];

the array's ___ ______. It indicates the number of elements, or values, the array can hold.

size declarator

A ________is used as an index to pinpoint a specific element within an array.

subscript

When processing the data in a two-dimensional array, each element has two ______: one for its row and another for its column.

subscripts

To _____all the elements of a two-dimensional array, you can use a pair of nested loops to add the contents of each element to an accumulator.

sum


Related study sets

History of Graphic Design - Test 1 review

View Set

Pentathlon Social Science Section 3 & 4

View Set

PREP U Chapter 10: Nursing Management: Patients With Chest and Lower Respiratory Tract Disorders

View Set

Quiz 1: Weather Factors of the Sky

View Set

SCI 2020 Chapter 14 Midterm Study Guide

View Set

Sect. 4.11 - Managing Headers, Footers, and Sections

View Set