Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays 1.

Similar presentations


Presentation on theme: "Arrays 1."— Presentation transcript:

1 Arrays 1

2 Outline Introduction Arrays Declaring and Allocating Arrays Examples Using Arrays   Allocating an Array   Initializing the Values in an Array   Summing the Elements of an Array   Using Arrays to Analyze Survey Results Using Histograms to Display Array Data Graphically Passing Arrays to Procedures Sorting Array Searching Arrays

3 Introduction Arrays Arrays are data structures consisting of data items of the same type. “Static” entities: They remain the same size once they are created Position number: Values that indicate specific locations within arrays The first element in every array is the zeroth element

4 Arrays Name of array (note that all elements of this array have the same name, numberArray) numberArray(0) -45 numberArray(1) 6 numberArray(2) numberArray(3) 72 numberArray(4) 1543 numberArray(5) -89 numberArray(6) numberArray(7) 62 numberArray(8) -3 Position number (index or subscript) of the element within array numberArray numberArray(9) 1 numberArray(10) 6453 numberArray(11) 78 Fig. 7.1 Array consisting of 12 elements.

5 Arrays numberArray.Length ‘ 12 numberArray.GetUpperBound(0) ‘11
All arrays have access to the methods and properties of class System.Array, including: Length property numberArray.Length ‘ 12 GetUpperBound method Returns the index of the last element in the array The value returned by this GetUpperBound is one less than the value of the array’s Length property numberArray.GetUpperBound(0) ‘11

6 Declaring and Allocating Arrays
Memory The amount of memory required by an array depends on the length of the array and the size of the data type of the elements in the array The declaration of an array creates a variable that can store a reference to an array but does not create the array in memory. To declare an array, the programmer provides the array’s name and data type. The following statement declares numberArray. Dim numberArray As Integer() The parentheses that follow the data type indicate that numberArray is an array.

7 Declaring and Allocating Arrays
Before the array can be used, the programmer must specify the size of the array and allocate memory for the array, using Keyword New: ‘ Declaration Dim numberArray As Integer() ‘ Allocation numberArray = New Integer(11) {} Array bounds : Determine what indices can be used to access an element in the array. Here ( 0 – 11)

8 Declaring and Allocating Arrays
numberArray = New Integer(11) { } The braces ({ and }) are called an initializer list and specify the initial values of the elements in the array. When the initializer list is empty, the elements in the array are initialized to the default value for the data type of the elements of the array. The default value is 0 for numeric primitive data-type variables, False for Boolean variables and Nothing for references.

9 Declaring and Allocating Arrays
Visual Basic can determine the array bounds from the number of elements in the initializer list. Thus, it is not necessary to specify the size of the array when a non-empty initializer list is present. Examples: Dim numbers As Integer() numbers = New Integer() {1, 2, 3, 6} ‘initialization Dim numberArray As Integer() = New Integer(11) {} Dim array1, array2 As Double()

10 Declaring and Allocating Arrays
How can we set up a Non-Fixed size array? First set up an array with empty brackets Dim numbers( 10) As Integer We can then use other value to reset the array. You reset an array by using the ReDim word. You then specify the new value. Like this: ReDim numbers(20)

11 Examples Using Arrays Several examples that demonstrate Declaration
Allocation Initialization of arrays

12 Allocate an array of 10 Integer elements,
1 ' Fig. 7.2: CreateArray.vb 2 ' Declaring and allocating an array. 3 4 Imports System.Windows.Forms 5 6 Module modCreateArray 7 Sub Main() Dim output As String Dim i As Integer 11 Dim array As Integer() ' declare array variable array = New Integer(9) {} ' allocate memory for array 14 output &= "Subscript " & vbTab & "Value" & vbCrLf 16 ' display values in array For i = 0 To array.GetUpperBound(0) output &= i & vbTab & array(i) & vbCrLf Next 21 output &= vbCrLf & "The array contains " & _ array.Length & " elements." 24 MessageBox.Show(output, "Array of Integer Values", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub ' Main 28 29 End Module ' modCreateArray Allocate an array of 10 Integer elements, which are initially zero. The program displays the array elements in tabular format in a dialog. Allocate an array of 10 elements using New and assigns it to array Appends to output the headings for the columns displayed by the program

13 Examples Using Arrays

14 1 ' Fig. 7.3: InitArray.vb 2 ' Initializing arrays. 3 4 Imports System.Windows.Forms 5 6 Module modInitArray 7 Sub Main() Dim output As String Dim i As Integer 11 Dim array1, array2 As Integer() ' declare two arrays 13 ' initializer list specifies number of elements ' and value of each element array1 = New Integer() {32, 27, 64, 18, 95, _ , 90, 70, 60, 37} 18 ' allocate array2 based on length of array1 array2 = New Integer(array1.GetUpperBound(0)) {} 21 ' set values in array2 by a calculation For i = 0 To array2.GetUpperBound(0) array2(i) = * i Next Creates two integer arrays of 10 elements each and sets the values of the elements, using an initializer list and a For structure. One statement is used to declare the two arrays Allocates the 10 elements of array1 with New and initialize the values in the array, using an initializer list Allocates array2, whose size is determined by arry1.GetUpperBound(0), so that array1 and array2 have the same upper bound Initializes each element in array2 to the even integers

15 Uses the values in the arrays to build String output, which is displayed in a MessageBox
26 output &= "Subscript " & vbTab & "Array1" & vbTab & _ "Array2" & vbCrLf 29 ' display values for both arrays For i = 0 To array1.GetUpperBound(0) output &= i & vbTab & array1(i) & vbTab & array2(i) & _ vbCrLf Next 35 MessageBox.Show(output, "Array of Integer Values", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub ' Main 39 40 End Module ' modInitArray

16 Declares, allocates and initializes the 10-element array, array
1 ' Fig. 7.4: SumArray.vb 2 ' Computing sum of elements in array. 3 4 Imports System.Windows.Forms 5 6 Module modSumArray 7 Sub Main() Dim array As Integer() = New Integer() _ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 11 Dim total As Integer = 0, i As Integer = 0 13 ' sum array element values For i = 0 To array.GetUpperBound(0) total += array(i) Next 18 MessageBox.Show("Total of array elements: " & total, _ "Sum the elements of an Array", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Sub ' Main 23 24 End Module ' modSumArray Sums the values contained in a 10-element integer array. Declares, allocates and initializes the 10-element array, array

17 1 ' Fig. 7.6: Histogram.vb 2 ' Using data to create histograms. 4 Imports System.Windows.Forms 5 Module modHistogram 7 Sub Main() Dim output As String ' output string Dim i, j As Integer ' counters 11 ' create data array Dim array1 As Integer() = New Integer() _ {19, 3, 15, 7, 11, 9, 13, 5, 17, 1} 15 output &= "Element " & vbTab & "Value " & vbTab & _ "Histogram" 18 For i = 0 To array1.GetUpperBound(0) output &= vbCrLf & i & vbTab & array1(i) & vbTab 21 For j = 1 To array1(i) output &= "*" ' add one asterisk Next 25 Next 27 MessageBox.show(output, "Histogram Printing Program", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub ' Main 31 32 End Module ' modHistogram Nested For loops append the bars to the String that is displayed in the MessageBox Inner For structure counts from 1 to array(i), which is the value in the ith index of array1

18

19 Array of Controls We can define an array of controls, using the same statements used to declare a variable array. For example, to declare an array of 20 buttons, use: Dim btnArray As Button() = New Button(20) { }

20 Array of Controls Say we have 10 check boxes (chkBox01, chkBox02, chkBox03, chkBox04, chkBox05, chkBox06, chkBox07, chkBox08, chkBox09, chkBox10) on a form and we need to examine each check box’s Checked property. If that property is True, we need to process 30 lines of additional code. For one check box, that code would be: If chkBox01.Checked Then [do these 30 lines of code] End If We would need to repeat this 9 more times (for the nine remaining check boxes), yielding a total of 32 x 10 = 320 lines of code.

21 Array of Controls Here’s the solution. Define an array of 10 check box controls and assign the array values to existing controls: Dim MyCheck As CheckBox() = new CheckBox(10) { } MyCheck(1) = chkBox01 MyCheck(2) = chkBox02 MyCheck(3) = chkBox03 MyCheck(4) = chkBox04 MyCheck(5) = chkBox05 MyCheck(6) = chkBox06 MyCheck(7) = chkBox07 MyCheck(8) = chkBox08 MyCheck(9) = chkBox09 MyCheck(10) = chkBox10 Dim I As Integer . For I = 1 To 10 If MyCheck(I).Checked Then [do these 30 lines of code] End If Next I

22 Array of Controls The 320 lines of code have been reduced to about 45 (including all the declarations) and code maintenance is now much easier. Obviously, it is not necessary to use control arrays, but they do have their advantages.


Download ppt "Arrays 1."

Similar presentations


Ads by Google