150 likes | 305 Views
Arrays. Array. “Regular” variable Holds a single value For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Student n as String If you have 10 students, you can also have variables representing students’ grades
E N D
Array • “Regular” variable • Holds a single value • For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentnas String • If you have 10 students, you can also have variables representing students’ grades • You can total up and average these grades for the “class” of 10 students ClassAvg = StudentGrade1 + StudentGrade2 + … • What happens if you have 1000 students?
Array • An array is a set of values that are logically related to each other • Monthly rainfall for the year • Number of students in each grade in a grade school • Number of employees in each department of an organization • Employees within a single department • Arrays let us refer to values by the same name • Individual items are referred to as elements and we use a number to indicate which specific element in the array we want • Element number is called the “index” or “subscript”
Elements of the "students" array Array • Dim Students(6) as Integer • Creates an array using the name “Students” with “containers” called “elements” • Elements start number at “0” • 7 elements exist in the Students array
Array • Assignment statements are used to place a value into a specific element • We just need to specify which element Students(4) = 18 • Places the value of 18 into the 5th element (we start counting at 0) of the array • Retrieving a value is as simple as specifying the element number • System.Console.WriteLine( Cstr( Students(4) ) )
Array • Assuming that we have an array “Students” which represents the grades kindergarten through 6th grade (kindergarten being grade “0”) Dim Students(6) as Integer Students(0)=11 Students(1)=14 Students(2)=17 Students(3)=11 Students(4)=19 Students(5)=21 Students(6)=10 Students 0 1 2 3 4 5 6 11 14 17 11 19 21 10
Array • We can get the total number of students in the school by adding the values in all the elements together TotalStudents = Students(0) + Students(1) + Students(2) + Students(3) + Students(4) + Students(5) + Students(6) • Typical processing of an array is by using a “LOOP” Dim Grade,TotalStudents as Integer TotalStudents = 0 For Grade = 0 to 6 TotalStudents = TotalStudents + Students(Grade) Next Grade Students 0 1 2 3 4 5 6 11 14 17 11 19 21 10
Arrays • The elements in an array are of the same data type • Integer • String • Boolean • Char • Etc • You cannot exceed the subscript range of the array • In “Students() ” you cannot refer to an element that does not exist • Typically done by “messing up” the loop counter • Produces a “run time” error For Grade = 0 to 7 TotalStudent=TotalStudents + Students(Grade) Next Grade
Initializing array at creation time Dim Students as Integer( ) = New Integer(6) {11,14,17,11,19,21,10} Integer( ) is the data type (array of integers) Students 0 1 2 3 4 5 6 11 14 17 11 19 21 10
Resizing an array • If you decide to include up to the 8th Grade ReDim Students(8) • Anything in the array will be lost!
Multidimensional arrays • Suppose we have a company with 100 employees • Each employee has an Employee ID # • A “whole counting number” • We wish to keep track of his/her salary • Could be a decimal • Create an array to hold this information Dim Employee(2,100) as Single • Create a 3 x 101 matrix • Note: data is of type “single” even though employee number is an integer • Salary is a decimal type (non-integer), so employee # 21 is 21.00 in the array • We don’t have to use the “0th” row or column, they can remain empty • Alternatively Dim Employee(1,99) as Single
Create an array of arrays • Add as many pairs of parentheses after the variable name as there are levels of nested arrays • Dim x as Integer( ) ( ) = New Integer( ) ( ) { } • Add the same number of paired parentheses to the “New” clause • You don’t need more that one pair of braces if you’re not giving an element values
Jagged arrays • Two dimensional, but not “rectangular” • Array of months • Each element is an array of days Dim sales()() As Double = New Double(11)( ) { } Dim month As Integer Dim days As Integer For month = 0 To 11 days = DateTime.DaysInMonth(Year(Now), month + 1) sales(month) = New Double(days - 1) {} Next month • declares an array variable to hold an array of arrays with elements of the Double Data Type • Each element or the array “sales” is itself an array representing a month • Each month holds values for each day in that month
Object Array:Array of different types • Technically you cannot do this • Arrays are of the same type • The type we will use is “Object” Dim x as Object( ) = New Object( ) { } • Example Dim employeeData(3) as Object employeeData(0) = “John Doe” employeeData(1) = “13 East North Rd.” employeeData(2) = 22 employeeData(3) = #04/01/1982# Age = CInt( employeeData(2) ) DOB = CDate( employeeData(3) ) • Negative performance consideration
Troubleshooting problems with arrays • Errors in declaring / initializing • Supplying the “new” clause after specifying dimension lengths • Omitting “new” when specifying element values • Using the “new” clause without braces • Out of bounds • Going beyond the upper or lower bounds of the dimensions • Specifying dimension • GetLowerBound and GetUpperBound methods are “0-based” • Lbound and Ubound are “1-based”