Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Array An array is a group of variables (called elements or components) containing values that all have the same type Index (or subscript) of the element in array c Name of the array: c c[ 0 ] 5 c[ 1 ] 55 c[ 2 ] -60 c[ 3 ] 84 c[ 4 ] 73 c[ 5 ] 2565 c[ 6 ] 0 c[ 7 ] 88 c[ 8 ] 7 c[ 9 ] 66 Slide 2
Declaring and Creating Arrays Array objects occupy space in memory. Like other objects, arrays are created with keyword new To create an array object, you specify the type of the array elements and the number of elements as part of an array-creation expression that uses keyword new The declaration and initialization can also be done as shown below Slide 3
Assigning Values to Array To refer to the elements, you use an index that ranges from zero (the first element in the array) to one less than the total number of elements If you specify an index that s less than zero or greater than the upper bound of the array, an ArrayIndexOutOfBoundsException will be thrown Slide 4
Accessing the Array s Values Slide 5
Array Initializer You can instantiate an array and provide initial values with an array initializer a comma-separated list of expressions (called an initializer list) enclosed in braces The number of values determines the size of the array type[] arrayname = { value1, value2, value3,... }; Slide 6
Getting Array s Size Slide 7
Using Loops With Array Slide 8
So An array can store more than one primitive type or object An element is one of the items in an array To create an array, you must declare a variable of the correct type and instantiate an array object that the variable refers to To declare an array variable, you code a set of empty brackets after the type or the variable name To instantiate an array, you use the new keyword and specify the length, or size, of the array in brackets following the array type You can specify the array length by coding a literal value or by using a constant or variable of type int When you instantiate an array of primitive types, numeric types are set to zeros and boolean types to false When you create an array of objects, they are set to nulls Slide 9
Exercise 01 Create an array of double data type to hold the list of 10 prices Use the for loop to display the values on screen Modify your code to display the sum of all the prices Slide 10
Exercise 02 Create a String array to hold the values of names of players in a cricket team, use Array Initializer for this purpose Display the names of the players using for loop Slide 11
Exercise 03 - Answer Slide 13
Enhanced for Statement The enhanced for statement iterates through the elements of an array without using a counter, thus avoiding the possibility of stepping outside the array The syntax o parameter: has a type and an identifier (e.g., int number) o arrayname is the array through which to iterate The type of the parameter must be consistent with the type of the elements in the array Slide 14
Example 01 Slide 15
Two Dimensional Arrays Slide 16
Two Dimensional Arrays Arrays that require two indices to identify a particular element are called two-dimensional arrays (Multidimensional arrays can have more than two dimensions.) Use two indexes and allow data to be stored in a table that consists of rows and columns In general, an array with m rows and n columns is called an m-by-n array Slide 17
Two-dimensional array with three rows and four columns Slide 18
Creating a rectangular array The syntax for creating a rectangular array type[][] arrayname = new type[rows][columns]; A statement that creates a 3x2 array int[][] numbers = new int[3][2]; Slide 19
How to assign values to a rectangular array The syntax for referring to an element of a rectangular array arrayname[rowindex][columnindex] The indexes for a 3x2 array [0][0] [0][1] [1][0] [1][1] [2][0] [2][1] Slide 20
How to assign values to a rectangular array Code that assigns values to the array numbers[0][0] = 1; numbers[0][1] = 2; numbers[1][0] = 3; numbers[1][1] = 4; numbers[2][0] = 5; numbers[2][1] = 6; Slide 21
Array Initializer Code that creates a 3x2 array and initializes it in one statement int[][] numbers = { {1,2}, {3,4}, {5,6} }; Slide 22
Code that processes a rectangular array with nested for loops Slide 23
End of Lecture