100 likes | 201 Views
Lecture 23: Arrays. Chapter 7: sections 7.1, 7.2. One Dimension Array. Is a COLLECTION of simple variables of the SAME type Declaration : Dim arrayName ( n ) as DataType Example: Dim grades(30) as Double Dim students(30) as String. s ubscript or index. Assigning values.
E N D
Lecture 23: Arrays Chapter 7: sections 7.1, 7.2
One Dimension Array • Is a COLLECTION of simple variables of the SAME type • Declaration: DimarrayName(n) asDataType • Example: Dim grades(30) as Double Dim students(30) as String subscript or index
Assigning values • To assign a new value, use the same technique as if you need to assign a value to a regular variable BUT don’t forget the SUBSCRIPT • students(2) =“Tom the Cat” • grade(1) =100 • The values can be assigned in the declaration step
One Dimension Array (cont.) • Before it is used as a variable, each array should have assigned a dimension (e.g., n in the previous example ) • An array variable declared in the Declaration section of the code window is a class-level variable • An array variable declared inside a procedure is local to the procedure and cease to exists when the procedure ends
Arrays: Declaration and Initialization • Different types of declaration: • 1. Dim country(10) as String : declares an array of Strings ; This array cannot have more than 10 elements • 2. Dim country() as String ={“USA”, “France”, “England”} : declares an array and assign 3 values for the first 3 cells. This array has length 3 • 3. Dim country() as String : declares an array of String elements. Before we assign a value we need to give it a dimension • ReDim country(10) : assigned a dimension • Different array types • You can create arrays of Controls (e.g. labels, buttons or textboxes) • Dim lblTitle(5) as Label : an array of lables
Useful functions and Properties • Length returns the length of the array. The last element is on index Length-1 • Students.Length 30 • GetUpperBound(0) returns the subscript of the last possible element in the array(it’s an INDEX not an element of the array) • Students.GetUpperBound(0) 30 • GetLowerBound(0) returns the subscript of the fist element in the array. Usually is 0
Useful functions and procedures • ReDim change the DIMENSION of an array after it has been declared and deletes all the elements that exists (it’s like declaring it again) • ReDim students(100) • ReDim Preserve change the dimension of an array but preserves the elements already in the array • ReDim Preserve students(100) • Split splits a string given a delimiter • strArray =line.Split(“,”c) • Join the reverse of the split function; returns a string value consisting of the elements of an array concatenated together and separated by the delimiter • Names =Join(nameArray , ”,”c)
Useful functions and properties • Make a duplicate array • arrayNew=arrayOne • NOTE: if you modify the elements in the arrayOne, arrayNew will be modified too. • Copy an array • Array.Copy(arrayOne, arrayNew, length) • Sort an array • Array.Sort(arrayName) • Search an element • itemIndex =Array.IndexOf(arrayName,value) • itemIndex =Array.IndexOf(arrayName, value, StartIndex)
Passing Arrays to a Procedures • An entire local array can be passed to a procedure • Declaring a procedure that has an array as parameter: • Function getFirst(ByVal names() as String) as String() • Calling a procedure with an array variable as arguments: • firstName=getFirst(students)
Comments: • How to use it: • Each individual variable is called an ELEMENT • Ex: students(3) is the 4th element of the students array • Using a subscript greater than the upper bound of an array is not allowed (same as with Strings)