320 likes | 515 Views
Royal University of Phnom Penh. Visual Basic Programming Array Lectured by: Mr. Kean Tak Prepared by: Group III. Content. I- Definition II- Types of Array 1. Fixed-Size Array 2. Dynamic Array III-Using Array 1. Passing Array as Parameter 2. Array Class 3. ArrayList Class.
E N D
Royal University of Phnom Penh Visual Basic Programming Array Lectured by: Mr. Kean Tak Prepared by: Group III
Content I- Definition II- Types of Array 1. Fixed-Size Array 2. Dynamic Array III-Using Array 1. Passing Array as Parameter 2. Array Class 3. ArrayList Class
I- Definition • An array is a collection of data with a single name. Items in an array are having the same type. Individual elements in an array are identified by means of an index, an integer.
II- Types of Arrays II- 1. Static Arrays ( Fixed-Size Arrays) Before create a fixed-size arrays, we have to be sure and limit the size for array. Then the size is set before the program runs.
Format for declaring a static array: - Dim or Public or Private is the keyword that declare the array - ArrayName is the variable name of the array - Dim1Index is the upper-bound of the first dimension of the array, which is the number of elements minus 1. - Dim2Index is the upper-bound of the second dimension of the array, which is the number of elements minus 1 (Additional dimension can be included if they are selected by comma). - DataType is a keyword corresponding of data that will be included in the array Dim|Public|PrivateArrayName(Dim1Index,Dim2Index,…) As DataType
Example: Dim Employees(9) As String or Dim Employees(0 To 9) As String Dim ScoreBoard(1,8) As Short or Dim Scoreboard(0 To 1, 0 To 8) As Short
We can input a value for an element of an array using a text box: Array1Name(2) = CInt(TextBox1.Text) Array2Name(3) = TextBox2.Text • We can change the values of individual elements of an array with assignments statements, like this: Array1Name(3) = 99 Array2Name (2) = "Mike"
The Fixed Array program uses the UBound function to check for the upper bound, or top index value of the array. LBound is also used to confirm the lower index value or lower bound of an array. LBound(ArrayName) UBound(ArrayName)
-The key word Preserve causes the values of the array t-o be preserved when the size is changed. ReDim Preserve ArrayName(ArrayIndex)
II- Types of Arrays II- 2. Dynamic Arrays: • Dynamic array is used when we don’t know the actual size of the array. The array size will be determined when the program runs.
Format for declaring a dynamic array: - Specify the name and type of the array in the program at design time, omitting the number of element in the array Dim|Public|PrivateArrayName() As DataType - Add code to determine the number of element that should be in the array at run time. Dim ArrayIndex As DataType
The variable in the ReDim statement to dimension the array, subtracting 1 because arrays are zero-based. ReDimArrayName(ArrayIndex-1)
Example: Dim Teperature() As Single Dim Day As Short Days= InputBox(“How many days?”, “Create array”) ReDimTeperrature(Days- 1)
III- Using Array III- 1. Passing Array as Parameter • We pass the name of the array to the method as the parameter and the result to be returned to the user of the method is a number – the sum of the values. • A call of the method looks like this: Dim sales(23, 11) As Integer Dim total As Integer total = Sum(sales)
The method itself is: Private Function Sum(ByVal array(,) As Integer) As Integer Dim total As Integer Dim row, col As Integer total = 0 For row = 0 To UBound(array,1) For col = 0 To UBound(array, 2) total = total + array(row, Col) Next Next Return total End Function
II-2. Array Class • Array Class provides a collection of methods that you can use to manipulate arrays while they are active in programs.
Methods for Arrays • BinarySearchOverloaded. Searches a one-dimensional sorted Arrayfor a value, using a binary search algorithm. • Clear Sets a range of elements in the Array to zero, to false, or to nullNothingnullptra null reference (Nothing in Visual Basic), depending on the element type. • Clone Creates a shallow copy of the Array. • ConstrainedCopy Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely. • ConvertAll((TInput, TOutput) Converts an array of one type to an array of another type.
CopyOverloaded. Copies a range of elements in one Array to another Array and performs type casting and boxing as required. • CopyTo Overloaded. Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array. • CreateInstance Overloaded. Initializes a new instance of the Array class. • Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.) • Exists(T) Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
FinalizeAllows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) • Find(T) Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array. • FindAll(T) Retrieves all the elements that match the conditions defined by the specified predicate. • FindIndex Overloaded. Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it. • FindLast(T) Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array.
FindLastIndexOverloaded. Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within an Array or a portion of it. • ForEach(T) Performs the specified action on each element of the specified array. • GetEnumerator Returns an IEnumerator for the Array. • GetHashCode Serves as a hash function for a particular type. (Inherited from Object.) • GetLength Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array. • GetLongLengthGets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
GetLowerBoundGets the lower bound of the specified dimension in the Array. • GetType Gets the Type of the current instance. (Inherited from Object.) • GetUpperBound Gets the upper bound of the specified dimension in the Array. • GetValue Overloaded. Gets the value of the specified element in the current Array. • IndexOf Overloaded. Returns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array. • Initialize Initializes every element of the value-type Array by calling the default constructor of the value type.
LastIndexOfOverloaded. Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. • MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.) • Resize(T) Changes the size of an array to the specified new size. • Reverse Overloaded. Reverses the order of the elements in a one-dimensional Array or in a portion of the Array. • SetValue Overloaded. Sets the specified element in the current Array to the specified value. • Sort Overloaded. Sorts the elements in one-dimensional Array objects. • ToString Returns a String that represents the current Object. (Inherited from Object.) • TrueForAll(T)Determines whether every element in the array matches the conditions defined by the specified predicate.
III-3.ArrayList Class • An ArrayList object is a sophisticated version of an array. The ArrayList class provides some features that are offered in most System.Collectionsclasses but are not in the Array class. Dim myAL As New ArrayList() myAL.Add("Hello") • The capacity of an Array is fixed, whereas the capacity of an ArrayList is automatically expanded as required. If the value of the Capacity property is changed, the memory reallocation and copying of elements are automatically done.
ArrayListprovide methods that add, insert, or remove a range of elements. In Array, you can get or set the value of only one element at a time. • A synchronized version of ArrayList is easy to create using the Synchronized method. The Array class leaves it up to the user to implement synchronization. • ArrayList provide methods that return read-only and fixed-size wrappers to the collection. Array does not.
On the other hand, Array offers some flexibility that ArrayList do not. For example: • You can set the lower bound of an Array, but the lower bound of an ArrayList is always zero. • An Array can have multiple dimensions, while an ArrayList always has exactly one dimension. • An Array of a specific type (other than Object) has better performance than an ArrayList because the elements of ArrayList are of type Object and, therefore, boxing and unboxing typically occur when storing or retrieving a value type.
Most situations that call for an array can use an ArrayList instead; they are easier to use and, in general, have performance similar to an array of the same type. • Array is in the System namespace; ArrayList is in the System.Collections namespace.
Methods for ArrayList • AdapterCreates an ArrayList wrapper for a specific IList. • AddAdds an object to the end of the ArrayList. • AddRange Adds the elements of an ICollection to the end of the ArrayList. • BinarySearch Overloaded. Uses a binary search algorithm to locate a specific element in the sorted ArrayList or a portion of it. • Clear Removes all elements from the ArrayList. • CloneCreates a shallow copy of the ArrayList. • Contains Determines whether an element is in the ArrayList. • CopyTo Overloaded. Copies the ArrayList or a portion of it to a one-dimensional array. • Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
FinalizeAllows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) • FixedSize Overloaded. Returns a list wrapper with a fixed size, where elements are allowed to be modified, but not added or removed. • GetEnumeratorOverloaded. Returns an enumerator that iterates through the ArrayList. • GetHashCode Serves as a hash function for a particular type. (Inherited from Object.) • GetRange Returns an ArrayList which represents a subset of the elements in the source ArrayList.
IndexOfOverloaded. Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it. • Insert Inserts an element into the ArrayList at the specified index. • GetType Gets the Type of the current instance. (Inherited from Object.) • InsertRange Inserts the elements of a collection into the ArrayList at the specified index. • LastIndexOf Overloaded. Returns the zero-based index of the last occurrence of a value in the ArrayList or in a portion of it. • MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.) • ReadOnlyOverloaded. Returns a list wrapper that is read-only.
RemoveRemoves the first occurrence of a specific object from the ArrayList. • RemoveAt Removes the element at the specified index of the ArrayList. • RemoveRange Removes a range of elements from the ArrayList. • Repeat Returns an ArrayList whose elements are copies of the specified value. • Reverse Overloaded. Reverses the order of the elements in the ArrayList or a portion of it. • SetRange Copies the elements of a collection over a range of elements in the ArrayList.
Sort Overloaded. Sorts the elements in the ArrayList or a portion of it. • SynchronizedOverloaded. Returns a list wrapper that is synchronized (thread safe). • ToArrayOverloaded. Copies the elements of the ArrayList to a new array. • ToString Returns a String that represents the current Object. (Inherited from Object.) • TrimToSize Sets the capacity to the actual number of elements in the ArrayList.