1/24
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Enhanced for statement:
Iterates through the elements of an array without using a counter.Avoids the possibility of “stepping outside” the array. Also works with the Java API’s prebuilt collections.
Enhanced for statement Syntax:
for (parameter : arrayName) {
statement }
where parameter has a type and an identifier and arrayName is the array through which to iterate.
Enhanced for statements Parameter type must be
consistent with the array’s element type.
The enhanced for statement simplifies
the code for iterating through an array.
The enhanced for statement can be used only to
obtain array elements. It cannot be used to modify elements. To modify elements, use the traditional counter-controlled for statement.Can be used in place of the counter-controlled for statement if you don’t need to access the index of the element.
To pass an array argument to a method,
specify the name of the array without any brackets. Since every array object “knows” its own length, we need not pass the array length as an additional argument.
To receive an array, the method’s parameter list must
specify an array parameter.
When an argument to a method is an entire array or an individual array element of a reference type, the called method receives
a copy of the reference.
When an argument to a method is an individual array element of a primitive type, the called method receives
a copy of the element’s value. Such primitive values are called scalars or scalar quantities.
Pass-by-value (sometimes called call-by-value):
A copy of the argument’s value is passed to the called method. The called method works exclusively with the copy. Changes to the called method’s copy do not affect the original variable’s value in the caller.
Pass-by-reference (sometimes called call-by-reference):
The called method can access the argument’s value in the caller directly and modify that data, if necessary. Improves performance by eliminating the need to copy possibly large amounts of data.
All arguments in Java are passed by
value.
A method call can pass two types of values to a method
Copies of primitive values, and Copies of references to objects. (Objects cannot be passed)
If a method modifies a reference-type parameter so that it refers to
another object, only the parameter refers to the new object. The reference stored in the caller’s variable still refers to the original object.
Although an object’s reference is passed by value, a method can still interact with the referenced object by calling
its public methods using the copy of the object’s reference. The parameter in the called method and the argument in the calling method refer to the same object in memory.
Two-dimensional arrays are often used to represent
tables of values with data arranged in rows and columns. Identify each table element with two indices. By convention, the first identifies the element’s row and the second its column.
Multidimensional arrays can have
more than two dimensions. Java does not support ________ arrays directly. Allows you to specify one-dimensional arrays whose elements are also one-dimensional arrays, thus achieving the same effect.
In general, an array with m rows and n columns is called
an m-by-n array.
Multidimensional arrays can be initialized with
array initializers in declarations.
A two-dimensional array b with two rows and two columns could be declared and initialized with nested array initializers as follows:
int[][] b = {{1, 2}, {3, 4}};
The initial values are grouped by row in braces. The number of nested array initializers (represented by sets of braces within the outer braces) determines the number of rows. The number of initializer values in the nested array initializer for a row determines the number of columns in that row. Rows can have different lengths.
The lengths of the rows in a two-dimensional array are not required to be the same:
int[][] b = {{1, 2}, {3, 4, 5}};
Each element of b is a reference to a one-dimensional array of int variables. The int array for row 0 is a one-dimensional array with two elements (1 and 2). The int array for row 1 is a one-dimensional array with three elements (3, 4 and 5).
A multidimensional array with the same number of columns in every row can be created with an array-creation expression.
int[][] b = new int[3][4]; 3 rows and 4 columns.
The elements of a multidimensional array are initialized when
the array object is created.
A multidimensional array in which each row has a different number of columns can be created as follows:
int[][] b = new int[2][]; // create 2 rows
b[0] = new int[5]; // create 5 columns for row 0
b[1] = new int[3]; // create 3 columns for row 1
Creates a two-dimensional array with two rows. Row 0 has five columns, and row 1 has three columns.
ended on page 39