click below
click below
Normal Size Small Size show me how
Chapter 8
Arrays; Introduction to Exception Handling
Term | Definition |
---|---|
Data Structures | Collections of related data items |
1. Arrays | Data structures consisting of related data items of the same type. An array is a group of variables called elements containing values that all have the same type |
2. Arrays | Arrays are fixed-length entities. Although an array variable may be reassigned such that it refers to a new array of a different length. |
Fixed-Length Properties | Remain the same length once they're created. |
Element's Index | To refer to a particular element in an array, we specify the name of the reference to the array and the position number of the element in the array, which is known as the element's index |
Array-Access Expression | Includes the name of the array, followed by the index of the particular element in square brackets([]). An app refers to any one of an array's elements with an array-access expression |
Zeroth Element | The first element in an array. Has the index zero. Thus, the elements of an array called c are c[0], c[1], c[2],... |
1. Declaring and Creating Arrays | To create an array object, you specify the type and number of array elements as part of an array-creation expressions that uses the keyword new. |
2. Declaring and Creating Arrays | Following declaration and array-creation expression create an array object containing 12 in elements and store the array's reference in c: int[] c = new int[12];... or... int[] c;... c = new int[12] |
Array Initializer | A comma-separated list of expressions(called an initializer list) enclosed in braces. An app can create an array and initialize its elements with an array initializer. Ex:.. int[] n = {10, 20, 30, 40 , 50};..Does not require a new to create array object |
Exception | A problem that occurs while a program executes |
Exception Handling | Enables you to create fault-tolerant programs that can resolve excptions |
try Statement | To handle an exception, place any code that might throw an exception in a try statement. |
try block | Contains the code that might throw an exception |
catch block | Contains the code that handles the exception that might be thrown in the corresponding try block. The braces that delimit the try and catch blocks are required |
foreach Statement | Iterates through the elements of an entire array or collection. The syntax is:. foreach(type identifier in arrayName).. statement..where type and identifier are the type and name f the iteration variable, and arrayName is array through which to iterate |
1. Passing an Array Element to a Method | To pass an array argument to a method, specify the name of the array without any brackets. For example, if hourlyTemperatures is declared as... double[] hourlyTemperatures = new double[24]... then the method call... |
2. Passing an Array Element to a Method | ModifyArray(hourlyTemperatures); ...passes the reference of array hourlyTemperatures to method ModifyArray. |
1. Specifying an Array Parameter | For a method to receive an array reference through a method call, the method's parameter list must specify an array parameter. For example, the method header ModifyArray might be written as... |
2. Specifying an Array Parameter | void ModifyArray(double[] b)...indicating that ModifyArray receives the reference of an array of double in parameter b. |
Test Harness(Test app) | Is responsible for creating an object of the class being tested and providing it with data. This data can be placed directly into an array with an array initializer, it can come from the user at the keyboard, or it can come from a file |
Two-Dimensional Arrays | Arrays that require two indices to identify a particular element. Often used to represent tables of values consisting of information arranged in rows and columns. By convention, the first indice identifies the elements row and second its column. |
C# Supported Two-Dimensional Arrays | C# supports two types of two-dimensional arrays - rectangular arrays and jagged arrays |
Rectangular Arrays | Rectangular arrays are used to represent tables of information in the form of rows and columns, where each row has the same number of columns.In general, an array with m rows and n columns is called an m-by-n array |
Array-Access Expression for a Two-Dimensional Rectangular Array | Every element in array a is identified by an array-access expression of the form a[row, column]...where a is the name of the array, and row and column are the indices that uniquely identify each element in array a by row and column number |
1. Array Initializer for a Two-Dimensional Rectangular Array | A rectangular array b with two rows and columns could be declared and initialized with nested array initializer as follows: int[ , ] b = {{1, 2}, {3, 4}};. |
2. Array Initializer for a Two-Dimensional Rectangular Array | The intializer values are grouped by row in braces. So 1 and 2 initialize b[0, 0] and b[0, 1], respectively, and 3 and 4 initialize b[1, 0] and b[1, 1] |
Jagged Arrays | Is maintained as a one-dimensional array in which each element refers to a one-dimensional array. The manner in which jagged arrays are represented makes them quire flexible, because the lengths of the rows in the array need not be the same. |
1. Array Initializer for a Two-Dimensional Array | We can access the elements in a jagged array by an array-access expression of the form arrayName[row][column] - similar to the array-access expression for rectangular arrays, but with a separate set of square brackets for each dimension. |
2. Array Initializer for a Two-Dimensional Array | A jagged array with three rows of different lengths could be declared and initialized as follows:...int[][] jagged = {new int[] {1, 2}, new int[] {3}, new int[] {4, 5, 6}};... |
3. Array Initializer for a Two-Dimensional Array | In this statement, 1 and 2 initialize jagged[0][0] and jagged[0][1], respectively; 3 initializes jagged[1][0]; and 4, 5, and 6 initialize jagged[2][0], jagged[2][1], and jagged[2][2], respectively. |
1. Creating Two-Dimensional Rectangular Arrays with Array-Creation Expressions | A rectangular array can be created with an array-creation expression. For example, the following lines declare variable b and assign it a reference to a three-by-four rectangular array:... int[ , ] b;... b = new int[3, 4]... |
2. Creating Two-Dimensional Rectangular Arrays with Array-Creation Expressions | In this case, we use the literal values 3 and 4 to specify the number of rows and number of columns, respectively, but this is not required - apps can also use variables and expressions to specify array dimensions |
1. Creating Two-Dimensional Jagged Arrays with Array-Creation Expressions | Each one-dimensional array in the jagged array must be initialized separately. A jagged array can be created as follows:... int[][] c;... c = new int[2][]... c[0] = new int[5];... c[1] = new int[3];... |
2. Creating Two-Dimensional Jagged Arrays with Array-Creation Expressions | The preceding statements create a jagged array with two rows, Row 0 has five columns, and row 1 and three columns |
Variable-Length Argument Lists | Allows creation of methods that receive arbitrary numbers of arguments. A one-dimension array-type argument preceded by keyword params in method's parameter list indicates that method receives variable number of arguments with type of array's the elements |
params | This use of a params modifier can occur only in the last entry of the parameter list. Ex: public static double Average(params double[] numbers)... {statements} |
D Format Specifier | In a format item, a D format specifier indicates that the value should be formatted as an integer, and the number after the D indicates how many digits this formatted integer must contain |