Level 66 Level 68
Level 67

[Quiz] Arrays


98 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
Definition of an array
An array is a sequence of values of the same type. They can hold both primitives and objects.
Syntax for initializing an array
typeName[] arrayReference = new typeName[length]
arrayReference[index]
Syntax for array element access
What field returns the number of elements in an array?
The length field. It is an instance variable of the array.
arrayReference[length - 1]
How do you access the last index of an array?
Initialize an integer array named primes with five values.
int[] primes = new int[] { 2, 3, 5, 7, 11 };
Only holds objects.
What makes an array list different from an array?
generic class
class parameterized by type of data it holds
Syntax for constructing an ArrayList?
ArrayList<type> reference = new ArrayList<type>();
What is BankAccount?
A type parameter. You cannot use primitive types as type parameters in an ArrayList.
Zero.
When you construct an ArrayList object, it has a size of?
Is arrayListReference[i] valid to get an element?
No. You must use the get method.
arrayListReference.size() - 1.
How do you get the last valid index of an ArrayList?
arrayListReference.set(i, a);
Called the set method. Overwrites index i with a. Nothing else is moved. Size stays the same.
arrayListReference.add(object);
Called the add method. Adds an object to the end of the array list and increases the size.
arrayListReference.add(i, a);
Called the add method. Adds object a at index i and moves all elements by one position, from the current element at position i to the last element in the array list. After each …
arrayListReference.remove(i);
Called the remove method. Removes the element at position i, moves all elements after the removed element down by one position, and reduces the size of the array list by 1.
arrayListReference.size();
Called the size method. Returns the size of the array list. The last valid index is arrayListReference.size() - 1.
Yes.
Do you have to import the ArrayList class to use it?
What is auto-boxing?
Conversion between primitive types and the corresponding wrapper classes is automatic.
What does an enhanced for loop (for each) do?
The enhanced for loop traverses all elements of a collection. You can't assign values with a for each loop.
Syntax of a for-each loop.
for (Type variable : collection)
Counting matches algorithm.
public class Bank
public class Bank
Finding a value algorithm.
BankAccount largestYet = accounts.get(0);
Finding max or min algorithm.
What is a 2D array?
Two-dimensional arrays form a tabular, two-dimensional arrangement. You access elements with an index pair a[i][j].
final int ROWS = 3;
Syntax for creating an array with 3 rows and 3 columns.
How to fill or search a 2D array.
for (int i = 0; i < ROWS; i++)
double[] prices = data;
Both arrays are now pointing to the same reference.
What does an array variable do?
An array variable stores a reference to the array. Copying the variable yields a second reference to the same array.
How do you copy the elements of an array?
Use the clone method to copy the elements of an array. Has a return type of Object.
Syntax of copying an array.
double[] prices = (double[]) data.clone();
What does System.arraycopy do?
Use the System.arraycopy method to copy elements from one array to another.
Syntax of System.arraycopy method.
System.arraycopy(from, fromStart, to, toStart, count);
data[i] = x;
Adds a new element at position i into data, first moves all elements from i onward one position up. Then inserts the new value. Last element is lost.
Removes an element at position i.
System.arraycopy(data, i + 1, data, i, data.length - i - 1);
double[] newData = new double[2 * data.length];
How to grow an array that has run out of space.
What are parallel arrays?
Two different arrays that should be combined into one array containing objects.
What is a test suite?
A test suite is a set of tests for repeated testing.
What is regression testing?
Regression testing involves repeating previously run tests to ensure that known failures of prior versions do not appear in new versions of the software.
Why are arrays important?
arrays help to improve *efficiency* in writing code
FACT
the statements *number++* and *++number* both increment the variable (281)
one (449)
primitive variables are designed to hold _______ value(s) at a time
array
public Polygon polygonFrom(Point... corners) {
does not (450)
declaring an array reference variable [does] or [does not] create an array
array [coded declaration & initialization]
int[] numbers = new int[6]; (450)
array size declarator
number inside the brackets of an array to declare the number of *elements* or *values* the array can hold (450)
subscript (such as for loop counters) (451)
a(n) _______ is used as an index to pinpoint a specific element within an array
variables (slide 9)
array subscripts *can be accessed* using _______
how to *input values* into an array
you must *input* the values one at a time into the individual array elements (452)
what does it mean when it says that Java performs *array bounds checking?*
to *not* allow a statement to use a subscript outside of the range of valid subscripts for an array (456)
off-by-one error
usually seen with loops, this error shows up as a result that is 1 less or 1 greater than the expected value (457)
int[] numbers;
2 different styles to use when declaring array reference variables include: [code]
the statement declares 3 variables
int [] numbers, codes, scores; [code]
int numbers[], codes, scores; [code]
the statement declares 3 variables, but only *numbers* is a reference to an int array
int numbers[], codes[], scores[]; [code]
to declare all 3 variables as references to int arrays using the alternate notation, you need to write a set of *[ ] brackets* after each variable name (460)
if (cost[20] < cost[0]) (462)
array elements may be used in relational expressions [code]
Array length [code]
double[] temperatures = new double[25]; (465)
length field
The _______ field can be useful when processing the entire contents of an array
warning
The compiler generates a _______ whenever a program uses a method, class, or field with the @Deprecated annotation
NOTE
you can *not* change the value of an array's length field (465)
enhanced for loop
designed to iterate once for every *element* in an *array*
enhanced for loop [code]
for (dataType elementVariable : array) {
enhanced for loop is simpler (466)
to access the values stored in an array, from the first element to the last element, which is simpler? [enhanced for loop] or [traditional for loop]
You need to:
you can *not* use the *enhanced for loop if:
int size;
use of an integer variable to specify an array's size declarator [code]
//Create array ref'd by numbers var
reassign an array reference variable to a different array [code]
CONCEPT
an array can be *passed as an argument to a method*
methods can be written to:
methods can be written to: [list]
showArray(numbers); (473)
pass an array as an argument by passing the value in the variable that references the array [code]
the actual array itself is *not passed*, but a *reference to the array is passed into the parameter* (473)
when an entire array is passed into a method, it is passed just as an object is passed. Why is this?
== operator (476)
you can*not* use _______ to compare 2 array reference variables to determine whether they are equal
accumulator variable (477)
use a(n) _______ to find the sum of the values in an array
partially filled array (487)
a _______ is normally used with an accompanying integer variable that holds the number of items stored in the array
reference (488)
a method can return a _______ to an array
String[] names = { "Bill", "Susan", "Steven", "Jean" }; (490)
statement that creates an array of *String objects* initialized with values [code]
an array of *String objects* is really _______
an array of references to *String objects* (491)
initialization list (492)
As with the primitive data types, a(n) _______ automatically causes an array of *String objects* to be created in memory
length method
counts the number of chars in a String
subscripts (496)
objects in an array are accessed with _______, just like any other data type in an array
search algorithm
method of locating a specific item in a larger collection of data (498, 501)
sequential search algorithm (498)
-if the value being searched for is *not* in the array, the algorithm unsuccessfully searches to the *end* of the array
sorting algorithm (501)
a _______ is a technique for scanning through an array and rearranging its contents in some specific order
binary search (505)
the _______ is much more efficient than the sequential search
requirements for a *binary search*
the values in the array must be sorted in ascending order (505)
declaring a *two-dimensional array*
2 sets of brackets & 2 size declarators are required:
one subscript for its row
when processing the data in a two-dimensional array, each element has 2 subscripts:
nested loops (511)
programs that process *two-dimensional arrays* can do so with _______
its own set of braces (513)
when initializing a *two-dimensional array*, you enclose each row's initialization list in _______
new (514)
as with one-dimensional arrays, you do *not* use the _______ key word when you provide an initialization list
length (514)
a one-dimensional array has a(n) _______ field that holds the number of elements in the array
reference (518)
when a two-dimensional array is passed to a method, the parameter must be declared as a(n) _______ to a two-dimensional array
ragged array
when the rows of a two-dimensional array are of different lengths (520)
dimensions (521)
Java does *not* limit the number of _______ that an array may have
three-dimensional array declaration [code]
double[] [] [] seats = new double[3] [5] [8]; (521)
main method (522)
when you invoke a Java program from the operating system *command line*, you can specify arguments that are passed into the _______ of the program
args[0] = "How"
java CommandLine How does this work? [code]
variable-length argument lists
-makes it possible to write a method that takes a variable number of arguments;