Moment 4 Flashcards
Ch 6 Arrays and ArrayLists Kapitel 6 (6.1 - 6.12)
Arrays (p. 246) are?
fixed-length data structures consisting of related data items of the same type.
Types in Java are divided into two categories, which?
—primitive types and reference types.
The primitive types are?
boolean, byte, char, short, int, long, float and double.
All other types are reference types, so classes, which specify the types of objects, are reference types.
A primitive-type variable can store?
exactly one value of its declared type at a time.
Reference-type variables (called references; p. 247) store the?
location of an object in the computer’s memory. Such variables refer to objects in the program.
A reference to an object (p. 247) is required to invoke an?
object’s methods. A primitive-type variable does not refer to an object and therefore cannot be used to invoke a method.
An array is a group of variables (called elements or components; p. 247) containing values that?
all have the same type. Arrays are objects, so they’re considered reference types.
A program refers to any one of an array’s elements with an array-access expression (p. 248) that includes?
the name of the array followed by the index of the particular element in square brackets ([]; p. 248).
The first element in every array has index?
zero (p. 248) and is sometimes called the zeroth element.
An index must be a nonnegative?
integer. A program can use an expression as an index.
An array object knows its own length and stores this information in a?
“length” instance variable
To create an array object, specify the array’s?
element type and the number of elements as part of an array-creation expression (p. 249) that uses keyword new.
When an array is created, each element receives a default value, which?
zero for numeric primitive-type elements, false for boolean elements and null for references.
In an array declaration, the type and the square brackets can be combined at the beginning of the declaration to indicate that?
all the identifiers in the declaration are array variables.
Every element of a primitive-type array contains a?
variable of the array’s declared type.
Every element of a reference-type array is a reference to an?
object of the array’s declared type.
A program can create an array and initialize its elements with an?
array initializer (p. 251).
Constant variables (p. 253) are declared with keyword?
final, must be initialized before they’re used and cannot be modified thereafter.
An exception indicates a?
problem that occurs while a program executes.
Exception handling (p. 259) enables you to?
create fault-tolerant programs.
When a Java program executes, the JVM checks array indices to ensure that?
they’re greater than or equal to 0 and less than the array’s length.
If a program uses an invalid array index, Java generates an?
exception (p. 259) to indicate that an error occurred in the program at execution time.
To handle an exception, place any code that might throw an exception (p. 259) in a?
“try” statement.
The “try” block (p. 259) contains the code that might throw an exception, and the “catch” block (p. 259) contains the
code that handles the exception if one occurs.
You can have many “catch” blocks to handle?
different types of exceptions that might be thrown in the corresponding “try” block.
When a “try” block terminates, any variables declared in the “try” block go?
out of scope.
A “catch” block declares a
type and an exception parameter. Inside the “catch” block, you can use the parameter’s identifier to interact with a caught exception object.
When a program is executed, array element indices are?
checked for validity
all array element indices must be greater than or equal?
to 0 and less than the length of the array.
If an attempt is made to use an invalid index to access an element, an?
“ArrayIndexOutOfBoundsException” (p. 259) exception occurs.
An exception object’s “toString” method returns the?
exception’s error message.
The enhanced for statement (p. 260) allows you to?
iterate through the elements of an array or a collection without using a counter.
The syntax of an enhanced “for” statement is:?
for (Type parameter : arrayName)
{
statement
}
The enhanced for statement cannot be used to
modify elements in an array.
If a program needs to modify elements, use the?
traditional counter-controlled for statement.
When an argument is passed by value, a copy of the argument’s value is made and passed to?
the called method. The called method works exclusively with the copy.
When an argument is passed by reference (p. 264), the called method can access the?
argument’s value in the caller directly and possibly modify it.
All arguments in Java are passed by?
value.
A method call can pass two types of values to a method, which?
copies of primitive values and copies of references to objects. Although an object’s reference is passed by value (p. 264), a method can still interact with the referenced object by calling its public methods using the copy of the object’s reference.
When you pass an array or an individual array element of a reference type to a method, the called method receives a?
copy of the array or element’s reference.
When you pass an individual element of a primitive type, the called method receives a copy of the element’s value.
To pass an individual array element to a method, use the?
indexed name of the array.
Multidimensional arrays with two dimensions are often used to?
represent tables of values consisting of information arranged in rows and columns.
two-dimensional array (p. 264) with m rows and n columns is called an?
m-by-n array.
An m-by-n array can be initialized with an array initializer of the form?
arrayType[][] arrayName = {{row1 initializer}, {row2 initializer}, …};
Multidimensional arrays are maintained as arrays of?
separate one-dimensional arrays. As a result, the lengths of the rows in a two-dimensional array are not required to be the same.
A multidimensional array with the same number of columns in every row can be created with an?
array-creation expression of the form
arrayType[][] arrayName = new arrayType[numRows][numColumns];
An argument type followed by an ellipsis (…; p. 268) in a method’s parameter list indicates that?
the method receives a variable number of arguments of that particular type.
The ellipsis can occur?
only once in a method’s parameter list. It must be at the end of the parameter list.
A variable-length argument list (p. 268) is treated as?
an array within the method body. The number of arguments in the array can be obtained using the array’s length field.
Passing arguments to main (p. 269) from the command line is achieved by?
including a parameter of type String[] in the parameter list of main. By convention, main’s parameter is named args.
Java passes command-line arguments that appear after the class name in the java command to the application’s “main” method as?
“Strings” in the array args.
Lists and tables of values can be stored in ____________ and _______________ .
arrays, collections.
An array is a group of _______________ (called elements or components) containing values that all have the same ______________.
variables, type.
The _____________ allows you to iterate through an array’s elements without using a
counter.
enhanced “for” statement.
The number used to refer to a particular array element is called the element’s _____________.
index (or subscript or position number).
An array that uses two indices is referred to as a(n) ___________ array.
two-dimensional.
Use the enhanced for statement _______________ to walk through “double” array numbers.
for (double d : numbers).
Command-line arguments are stored in _____________.
an array of “Strings”, called “args” by convention.
Use the expression _____________ to receive the total number of arguments in a command line. Assume that command-line arguments are stored in String[] args.
args.length.
Given the command java MyClass test, the first command-line argument is _______________.
test.
A(n) __________ in the parameter list of a method indicates that the method can receive a variable number of arguments.
ellipsis (…).