royal university of phnom penh

32
Royal University of Phnom Penh Visual Basic Programming Array Lectured by: Mr. Kean Tak Prepared by: Group III

Upload: jariah

Post on 23-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Royal University  of  Phnom Penh

Royal University of Phnom Penh

Visual Basic Programming

ArrayLectured by: Mr.

Kean TakPrepared by: Group III

Page 2: Royal University  of  Phnom Penh

ContentI- DefinitionII-Types of Array

1. Fixed-Size Array2. Dynamic Array

III-Using Array1. Passing Array as Parameter2. Array Class3. ArrayList Class

Page 3: Royal University  of  Phnom Penh

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.

Page 4: Royal University  of  Phnom Penh

II- Types of ArraysII- 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.

Page 5: Royal University  of  Phnom Penh

• 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|Private ArrayName(Dim1Index,Dim2Index,…)

As DataType

Page 6: Royal University  of  Phnom Penh

• Example:

Dim Employees(9) As Stringor Dim Employees(0 To 9) As String

Dim ScoreBoard(1,8) As Shortor Dim Scoreboard(0 To 1, 0 To 8) As Short

Page 7: Royal University  of  Phnom Penh

• 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"

Page 8: Royal University  of  Phnom Penh

• 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)

Page 9: Royal University  of  Phnom Penh

-The key word Preserve causes the valuesof the array t-o be preserved when the size is changed.

ReDim Preserve ArrayName(ArrayIndex)

Page 10: Royal University  of  Phnom Penh

II- Types of ArraysII- 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.

Page 11: Royal University  of  Phnom Penh

• 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|Private ArrayName() As DataType- Add code to determine the number of element that should be in the array at run time.

Dim ArrayIndex As DataType

Page 12: Royal University  of  Phnom Penh

-The variable in the ReDim statement to dimension the array, subtracting 1 because arrays are zero-based.

ReDim ArrayName(ArrayIndex-1)

Page 13: Royal University  of  Phnom Penh

• Example: Dim Teperature() As SingleDim Day As ShortDays= InputBox(“How many days?”, “Create array”)ReDim Teperrature(Days- 1)

Page 14: Royal University  of  Phnom Penh

III- Using ArrayIII- 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 Integertotal = Sum(sales)

Page 15: Royal University  of  Phnom Penh

• 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 totalEnd Function

Page 16: Royal University  of  Phnom Penh

II-2. Array Class

• Array Class provides a collection of methodsthat you can use to manipulate arrays while they are active in programs.

Page 17: Royal University  of  Phnom Penh

Methods for Arrays

• BinarySearch Overloaded. Searches a one-dimensional sorted Array for a value, using a binary search algorithm.

• Clear Sets a range of elements in the Array to zero, to false, or to nullNothingnullptr a 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.

Page 18: Royal University  of  Phnom Penh

• Copy Overloaded. 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.

Page 19: Royal University  of  Phnom Penh

• Finalize Allows 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.

Page 20: Royal University  of  Phnom Penh

• FindLastIndex Overloaded. 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.• GetLongLength Gets a 64-bit integer that represents the number of

elements in the specified dimension of the Array.

Page 21: Royal University  of  Phnom Penh

• GetLowerBound Gets 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.

Page 22: Royal University  of  Phnom Penh

• LastIndexOf Overloaded. 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.

Page 23: Royal University  of  Phnom Penh

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.Collections classes 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.

Page 24: Royal University  of  Phnom Penh

• ArrayList provide 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.

Page 25: Royal University  of  Phnom Penh

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.

Page 26: Royal University  of  Phnom Penh

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.

Page 27: Royal University  of  Phnom Penh

Methods for ArrayList

• Adapter Creates an ArrayList wrapper for a specific IList.• Add Adds 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. • Clone Creates 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.)

Page 28: Royal University  of  Phnom Penh

• Finalize Allows 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.

• GetEnumerator Overloaded. 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.

Page 29: Royal University  of  Phnom Penh

• IndexOf Overloaded. 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.) • ReadOnly Overloaded. Returns a list wrapper that is read-only.

Page 30: Royal University  of  Phnom Penh

• Remove Removes 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.

Page 31: Royal University  of  Phnom Penh

• Sort Overloaded. Sorts the elements in the ArrayList or a portion of it.

• Synchronized Overloaded. Returns a list wrapper that is synchronized (thread safe).

• ToArray Overloaded. 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.

Page 32: Royal University  of  Phnom Penh

Thanks For Attention

Q&A