lecture 10 - arrays
TRANSCRIPT
-
8/14/2019 Lecture 10 - Arrays
1/14
Lecture 10 - Arrays
-
8/14/2019 Lecture 10 - Arrays
2/14
Outline
In the next few lectures, we will discuss VB.NET data structures
In which we combine several primitive data types:i.e., of either the same, or different types.together, to make a more complicated data structure.
We will look at:[Lecture 10 ] Arrays (weve already seen these, as lists )
In which we construct and use an indexed array of objectsall of the same type (e.g., Integers).
[Lectures 11 - 13] Enumerations and Structures (etc)Which allow us to:
Carefully limit our basic data types (Enumerations)Combine different data types, to form collectives (Structures)
-
8/14/2019 Lecture 10 - Arrays
3/14
Introduction
It is often useful to create, store, and process lists of related data:In which each member of the list has the same Data Type .
Example: A list of Student Names (all of type, String).We already saw such a list when we used the VB .NET ListBox control.
In VB .NET, such structures are called Arrays
VB. NET Arrays have many of the usual array characteristics:Each member of the array is called an Array Element .
All array elements must be of the same Data Type.The total number of elements is called the Size of the array.
Each array element is assigned a numeric index :A number used to reference (to access) the array element.
All Arrays in VB .NET are zero indexed ;This just means that the elements are ordered from 0 to Size - 1 .
Example: an array of size four has 4 elements, ordered from 0 to 3 .
-
8/14/2019 Lecture 10 - Arrays
4/14
-
8/14/2019 Lecture 10 - Arrays
5/14
Example: Simple ArrayLets write a VB .NET program, to implement our array:
-
8/14/2019 Lecture 10 - Arrays
6/14
In the last example, we could have used a ForNext LoopTo add names to our ListBox, one by one, via looping.This seems more efficient, but requires some advance-knowledge:
We must know (and specify) the number of elements.
VB .NET provides the For EachNext loop for this job!This loop structure is similar to a ForNext loop ;
Repeated execution of a set of statements.
However, it automatically visits each element, one-by-oneFrom the first to the last (0 to 3, in our simple example).
The VB syntax for the ForNext loop is:
Dim elementName As DataType For Each elementName In structure
statements Next
Here, elementName is a loop-variable of type DataType (i.e., String )which is used to store the current element in each loop cycle.
In is a VB .NET keyword that specifies to look in structure
And pass elements of type DataType to variable, elementName .Lets look at a flow-chart and example
The For EachNext Loop
-
8/14/2019 Lecture 10 - Arrays
7/14
Example: Add each friend name in array, friends to our ListBox.We use a For EachNext loop
With the loop variable, friendName .And the array, friends .
VB .NET code:
Dim friendName As String For Each friendName In friends
lstFriends.Items.Add(friends(i)) Next
The For EachNext loops flowis shown using a Flow Chart :Our loop is automatically executed 4 times.
In effect: For i = 0 To 3 Step 1.In cycle i , friendName = friends( i );
This is kindly done automatically for us.
After 4 cycles, we have no more elements , and the loop is finished.
Flow Chart: For EachNext Loop
friendName
-
8/14/2019 Lecture 10 - Arrays
8/14
-
8/14/2019 Lecture 10 - Arrays
9/14
In VB .NET, we may pass an array into a Method :By passing the Array as a ( Function or Subroutine ) parameter.
Note: Even though the parameter is passed ByVal.Accomplished by stating, As DataType() after the parameter name
Example: Sub OurSub( ByVal parameterName As String() )
Here, As String() advises the system to accept a String Array;Note the use of empty parentheses : size is NOT SPECIFIED.
Example: lets write a subroutine to Sort and Display friend names :
Passing Arrays as Parameters
-
8/14/2019 Lecture 10 - Arrays
10/14
Ex: Passing Arrays (and Sorting)Lets implement our sorting subroutine,
by updating our code:
-
8/14/2019 Lecture 10 - Arrays
11/14
You may determine the size of an Array, during run-timeUsing a pre-defined Function of Class Array: GetUpperBound( 0)
Example:
Dim upperBnd As Integer = friends .GetUpperBound( 0 )
Here, use of parameter, 0 specifies the upper-bound
The max number of elements supported by the arrays first dimension(Arrays can be multi-dimensional )
In our case, upperBnd is assigned the value 3.
Note: use of the dot ( . ) operator.
The resulting value could be used to implement a ForNext loopFor displaying array values to a ListBox,
Instead of the For EachNext loop, that we used earlier.
Determining Array Size
-
8/14/2019 Lecture 10 - Arrays
12/14
You may initialize an array, during its declaration
As a comma-delimited list of elements, surrounded by curly-brackets .Example:
Dim friends(3) As String = {Charlie, Snoopy, Woodstock, _ Schroeder }
Here, this declares a String array called friend,And initializes it to contain the 4 desired elements (in order):
friends(0) = Charliefriends(1) = Snoopyfriends(2) = Woodstockfriends(3) = Schroeder
Note: With explicit bounds, the size is omitted (not friends( 3)).Note that in VB .NET, we use an underscore ( _ )
to continue a long statement to another line.
Array Initialization
-
8/14/2019 Lecture 10 - Arrays
13/14
Dynamic ArraysYou may freely re-size an array, using the Redim keyword
Where you specify an integer n, the desired new size of the array.Note: here, Redim is short for re-dimension.For instance:
ReDim friends(9)
This statement re-sizes the array, friends() To size 10 (i.e., friends now has space for 10 elements, not 4).
Side Effect: Unfortunately, ReDim also clears the array , by default.Actually, Redim destroys the old array, and gives us a new one
With the same size, but empty.To prevent this, you must use the Preserve keyword with ReDim .
For instance:ReDim Preserve friends(9)
This again, creates a new 10 element array, friends (previously size 4)but now also copies the stored information (so you dont lose it).
-
8/14/2019 Lecture 10 - Arrays
14/14
Our examples today have used one-dimensional arrays However, VB .NET also supports multi-dimensional arrays .
A multidimensional array is declared straightforwardly:By specifying additional dimensions, as shown via the 2-D array below.
You may specify up to 32 dimensions (fixed once declared)
However: A key limitation of N-dimension arrays is their uniformity ...
All array elements must be of the same data type !While most real objects display characteristics that are a mixture of types.
Thus, use of these structures has become less common
Multidimensional Arrays