Java Chapter 7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

T/F Java does not limit the number of dimensions that an array may have.

True

T/F Once an array is created, its size cannot be changed

True

T/F to copy an array you need to copy the individual elements of one array to another. Usually, this is best done with a loop

True

True or False: Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.

True

True or False: The Java compiler does not display an error message when it processes a statement that uses an invalid subscript

True

Using the == operator to compare two arrays. You cannot use the == operator to compare two array reference variables and determine whether the arrays are equal. When you use the == operator with reference variables, the operator compares the __________________ that the variables contain, not the contents of the objects referenced by the variables.

memory addresses

When you use the == operator with reference variables, including those that reference arrays, the operator compares the __________that the variables contain, not the contents of the objects referenced by the variable

memory addresses

Bounds checking occurs at ________. 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 ______________

runtime, throws an exception and immediately terminates.

The first subscript in an array is always ___________.

0

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

0 to (n-1)

The last subscript in an array is always __________.

1 less than the number of elements

When an ArrayList object is first created, using the no-arg constructor, it has an initial capacity of____ items.

10

Selection sort algorithm

A sort algorithm that repeatedly scans for the smallest item in the list and swaps it with the element at the current index. The index is then incremented, and the process repeats until the last two elements are sorted.

Binary search algorithm

An algorithm that searches for a target value in a sorted list by checking at the midpoint and then repeatedly cutting the search section in half.

The _______________ for loop can be used to display all items in an ArrayList

enhanced

Here is the general format of the enhanced for loop:

for (dataType elementVariable : array) statement; for (int val: numbers) System.out.println(val);

import statment needed to use ArrayList class

import java.util.ArrayList

To sum a row of a 2D array, the accumulator is set to zero just before the _____________loop executes

inner

As with any other type of object, it is possible to declare a reference variable and create an instance of an array with one statement. Give an example

int[] numbers = new int[6];

Here is an example of a statement that declares an array reference variable:

int[] numbers;

two different styles when declaring array reference variables.

int[] numbers; or int numbers[]; if 3 arrays int[] numbers, codes, scores; or int numbers[], codes[], scores[];

An array's size declarator must be a non-negative_________ expression.

integer

Variable length argument lists

method can take a variable number of arguments

example of using add method for ArrayList class

nameList.add("James");

The ArrayList class has an overloaded version of the add method that allows you to add an item at a specific index. This causes the item to be inserted into the ArrayList object at a specific position

nameList.add(1, "Mary");

ArrayList remove method

nameList.remove(1) //removes item at index 1

You can specify a different starting capacity, if you desire, by passing an int argument to the ArrayList constructor. For example, the following statement creates an ArrayList object with an initial capacity of 100 items:

ArrayList list = new ArrayList(100)

How to create an ArrayList object

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

How do you initialize a 2D array?

Enclose each row's initialization list in it's own set of braces. Ex. int[][] numbers={{1,2,3}, {4,5,6}, {7,8,9}};

Each array in Java has a public field named length.

Ex. double[] temperatures = new double[25]; size = temperatures.length; (assigns 25 to size)

double[] [] scores = new double [3] [4];

Example declaration for 2 dimensional array

True or False: An array's size declarator can be a negative integer expression.

False

True or False: The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows.

False

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?

Finds smallest value and moves to index 1

An array's size declarator____________________

It indicates the number of elements, or values, the array can hold.

Binary Search Algorithm Pros and cons

More efficient but list must be sorted.

This is the typical number of comparisons performed by the sequential search on an array of N elements (assuming the search values are consistently found).

N/2

When an individual element of an array is passed to a method

The method does not have direct access to the original array

T/F An array can be passed as an argument to a method. To pass an array, you pass the value in the variable that references the array.

True

T/F By default, Java initializes array elements with 0.

True

T/F Declaring an array reference variable does not create an array.

True

T/F Each element in the numbers array, when accessed by its subscript, can be used as an int variable.

True

T/F The largest subscript in an array is length - 1

True

T/F When an entire 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.

True

T/F When an item is removed from an ArrayList, the items that come after 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.

True

T/F You can create an ArrayList to hold any type of object

True

T/F You can write a method to accept a mixture of fixed arguments and variable arguments

True

T/F You may create arrays of objects that are instances of classes that you have written

True

True or False: A two-dimensional array has multiple length field

True

True or False: An ArrayList automatically expands in size to accommodate the items stored in it

True

True or False: Both of the following declarations are legal and equivalent: int[] numbers; int numbers[];

True

True or False: The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array

True

True or False: The values in an initialization list are stored in the array in the order that they appear in the list.

True

True or False: When an array is passed to a method, the method has access to the original array

True

T/F Array elements may also be used in relational expressions.

True Ex. if (cost[20] < cost[0])

T/F Java allows you to initialize an array's elements when you create the array.

True Note that you do not use the new key word when you use an initialization list.

This type of array is an array of arrays. Thought of as having rows and columns

Two-dimensional array

If an array is an array of four uninitialized String references., because they do not reference any objects, they are set to_______

null.

Statement that creates an int array

numbers = new int[6];

Because array subscripts start at 0 rather than 1, you have to be careful not to perform an ______________

off-by-one error.

Method header for passing 2D array

private static void showArray(int[][] array)

vararg parameter

public static int sum(int...numbers)

A 2D array that has rows of different lengths

ragged array

When you pass an array as an argument, you simply pass the value in the variable that________________ the array

references

an array of String objects is really an array of ___________ to String objects.

references

A______________ algorithm is a method of locating a specific item in a larger collection of data.

search

A __________ is a method of locating a specific item in a larger collection of data.

search algorithm

The _________ search algorithm uses a loop to sequentially step through an array, starting with the first element.

sequential

Selection sort pros and cons

simple but inefficient, N/2 attempts

To determine the number of items stored in an ArrayList object, you use this method.

size

In an array declaration, this indicates the number of elements that the array will have

size declarator

The number inside the brackets is the array's ___________________

size declarator.

A _______________ algorithm is used to arrange data into some order

sorting

Each element in an array is assigned a unique number known as a ________.

subscript

Each element of an array is accessed by a number known as a(n) __________

subscript

This set of empty angled brackets ( <> ) is called___________. It causes the com-piler to infer the required data type from the reference variable declaration

the diamond operator

Array bounds checking happens __________.

when the program runs

What would be results after following code was executed? Int [] x={23,55,83,19}; Int[] y={36,78,12,24}; x=y; y=x;

x[]={36,77,12,24} y[]={36,77,12,24}

Subscript numbering always starts at _____

zero.

What cases can you not use the enhanced for loop and should therefore use a traditional loop to work with arrays

• if you need to change the contents of an array element • if you need to work through the array elements in reverse order • if you need to access some of the array elements, but not all of them • if you need to simultaneously work with two or more arrays within the loop • if you need to refer to the subscript number of a particular element

To insert an item at a specific location in an ArrayList object, you use this method.

add

public static void main (String [] args) Inside the parentheses of the method header is the declaration of a parameter named args. This parameter is an array name. As its declaration indicates, it is used to reference an _________________

array of Strings.

Sequential search number of comparisons

average n/2, worst case n, best case 1

This search algorithm repeatedly divides the portion of an array being searched in half

binary search

What type of loop is often used to process 2D arrays?

Nested loops

T/F It is possible to reassign an array reference variable to a different array

True

T/F Java performs array bounds checking, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.

True

both the array1 and array2 variables will reference the same array. This type of assignment operation is called a reference copy.

it makes a copy of the address that is stored in array1 and stores it in array2.

To delete an item from an ArrayList object, you use this method.

remove

The first size declarator in 2D array specifies number of ________. Second specifies number of _____________

rows columns

T/F An array's size declarator can be a literal value or a variable

True

T/F If a partially filled array is passed as an argument to a method, the variable that holds the count of items in the array must also be passed as an argument. Otherwise, the method will not be able to determine the number of items that are stored in the array.

True

Use of length field in 2D array. numbers.length holds number of rows in array. numbers[row].length holds number of columns in the row.

for (int row=0;row<numbers.length;row++) { for (int col=0;col<numbers[row].length;col++) statements... }

The ArrayList class's set method can be used to replace an item at a specific index with another item. For example, the following statement will replace the item currently at index 1 with the String "Becky":

nameList.set(1, "Becky");

toString method of ArrayList

returns a string representing all of the items stored in an ArrayList object [James, Catherine, Bill]

This search algorithm steps through an array, comparing each item with the search value

sequential search

The sequential search algorithm returns __________ if the search value is not found

-1 (Because -1 is not a valid subscript)

With the enhanced for loop you do not have to be concerned about the size of the array, and you do not have to create an "index" variable to hold subscripts.

Advantages of enhanced for loop

This array field holds the number of elements that the array has.

length

ArrayList size method

returns the number of items stored in an ArrayLIst

An array can hold multiple values of the ___________data type simultaneously

same

T/F Java allows you to spread the initialization list across multiple lines.

True

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

import java.util.ArrayList

T/F In addition to accepting arrays as arguments, methods may also return arrays

True

ArrayList class

class that provides a dynamically resizable container that stores lists of objects.

When initializing a two-dimensional array, you enclose each row's initialization list in

braces

ArrayList get method

returns the item stored at specific index


Set pelajaran terkait

Week 13: Water and Electrolytes: Striking a Balance (CH. 9)

View Set

Rocks & Minerals: Metamorphic Rocks

View Set

Investments Exam 3 Chapter Questions

View Set

BIO 1013 A&P2 Heart and Blood Vessels Chap 20,21 Test

View Set

AIC 30 #9 -- Good Faith Claims Handling

View Set

WGU D265 Critical Thinking: Reasoning and Evidence - Practice Assessment

View Set