0% found this document useful (0 votes)
20 views

JK VB Net 8 1 Arrays

Jk Vb Net 8 1 Arrays

Uploaded by

curtisandrea242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

JK VB Net 8 1 Arrays

Jk Vb Net 8 1 Arrays

Uploaded by

curtisandrea242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Arrays

Arrays
Topics
•Declare and initialize a one-dimensional array
•Compute the sum and average of a one-dimensional array
•Sort a one-dimensional array
•Using For… Next with array
Write a program that displays the content of an array named
arrayNames in a listBox. (Make use of a listBox named
lstArray and a Button control named bntAddArray.)

i. Sketch the GUI of the controls to be used in the


program
ii. Write code that will declare the array arrayNames
which should hold three elements of type string (ie
three Names)
iii. Assign each element of the array to a Name of one of
your classmates

iv. Make use of a for loop to display the content of the


array in the listBox
Introduction
• Arrays are like groups of variables that allow you to store sets
of similar data
– A single dimension array is useful for storing and working
with a single set of data, like a column or a row of data.
– A multidimensional array can be used to store and work
with multiple sets of data. A two-dimensional array deals
with data in a table.
Array Characteristics

• An array stores multiple values of same type


• Like a group of variables with a single name
• For example, the days of the week might be:
– a set of 7 string variables
• All variables within an array are called elements and
must be of the same data type
• You access the elements in an array through a
subscript/ index
Subscript Characteristics

• A subscript, also called an index, is a number that


identifies a specific element within an array
• Subscript/index numbering works like a list box index:
– Subscript numbering begins at 0
– 1st element in an array is always subscript 0
– Last element is total number of elements – 1
• An array with 7 elements refers to the 1st element as
subscript 0 and the last element as subscript 6
Declaring a One-Dimensional Array
Dim ArrayName(UpperSubscript) As DataType

• ArrayName is the variable name of the array


• UpperSubscript is the value of the array's
highest subscript
– Must be a positive whole number
• DataType may be any Visual Basic data type
• VB knows an array is declared when name is followed
by parentheses with number of elements
Array Declaration Example

Dim intHours(5) As Integer

intHours(0) intHours(1) intHours(2) intHours(3) intHours(4) intHours(5)

• Note that 6 elements are available when an


array with an upper subscript of 5 is declared
• Each element is an integer type
Default Initialization
• All elements of an Integer array are initialized
to zero
– Same initialization as an integer variable
• Each array element is initialized exactly the
same as a simple variable of that data type
Implicit Array Sizing & Initialization

• An array can be initialized when declared


• Example:
Dim intNumbers() As Integer = {2, 4, 5, 7, 9, 12}

• This array is implicitly sized


– Upper subscript value is left blank
– Number of elements implied from initialization
– Upper subscript of 5 implied by this example
– This results in a 6 element array
• Elements are assigned the values shown
Working With Array Elements
• Simply include the subscript to use an array element
like an ordinary variable
• intNumbers(2) refers to 3rd element of array
intNumbers(0) = 100
intNumbers(1) = 200
intNumbers(2) = 300
intNumbers(3) = 400
intNumbers(4) = 500
intNumbers(5) = 600

decPay = intHours(3) * decRate

intNumbers(0) += 1

MessageBox.Show(decPay(5))
Arrays and Loops

• Loops are frequently used to process arrays


• Use a loop to prompt for 10 values
Dim intNbrs(9) as Integers
Dim intCount As Integer
For intCount = 0 To 9
intNbrs(intCount) = CInt(InputBox("Enter number"))
Next intCount

• Use a loop to reset values in the strNames


array to an empty string Dim strNames(999) as String
Dim intCount As Integer
For intCount = 0 To 999
strNames(intCount) = ""
Next intCount
Array Bounds Checking
• Visual Basic checks each subscript value of each
array reference used at run time
• A subscript is invalid if it is
– Negative
– Greater than the UpperSubscript declared
• An invalid subscript causes VB to throw a run-time
exception
• Bounds checking is not done at design time
Determining Number of Elements

• Arrays have a Length property that holds the number


of elements in the array
Dim intValues(25) As Integer

For intCount = 0 to (intValues.Length – 1)


MessageBox.Show(intValues(intCount))
Next intCount

• Note that length is number of array elements, not the


upper subscript of the array
• Length property always 1 greater than the upper
subscript
Total the Values in an Array

• Variable to hold sum (intTotal)is initialized


to zero prior to loop
• Iterate over all elements, adding each element
to intTotal
Dim intUnits(24) as Integer 'Declare array
Dim intTotal As Integer = 0 'Initialize accumulator
Dim intCount as Integer 'Declare loop counter

'Find total of all values held in array


For intCount = 0 To (intUnits.Length – 1)
intTotal = intTotal + intUnits(intCount)
Next
Average the Values in an Array

• Similar to previous example but then divides total by


number of elements to get average
Dim intUnits(24) as Integer 'Declare array
Dim intTotal As Integer = 0 'Initialize accumulator
Dim dblAverage as Double 'Declare average var
Dim intCount as Integer 'Declare loop counter

'Find total of all values held in array


For intCount = 0 To (intUnits.Length – 1)
intTotal += intUnits(intCount)
Next intCount

'Use floating-point division to compute average


dblAverage = intTotal / intUnits.Length
Find Highest & Lowest Array Values

• Pick first element as the highest


• Search rest of array for higher values
• If a higher value is located, save the value
Dim intNumbers(24) as Integer
Dim intCount as Integer
Dim intHighest as Integer = intNumbers(0)

For intCount = 1 To (intNumbers.Length - 1)


If intNumbers(intCount) > intHighest Then
intHighest = intNumbers(intCount)
End If
Next intCount

• Use similar logic to find lowest value


Sorting an Array, Descending
• Use the Sort method in conjunction with the Reverse
method
• Arranges elements in descending order (highest to
lowest)
• Sort intNumbers in this order: {12, 7, 6, 3, 1}
Dim intNumbers() As Integer = { 7, 12, 1, 6, 3 }
Array.Sort(intNumbers)
Array.Reverse()
• Sort strNames in this order: {Sue, Kim, Bill, Alan}
Dim strNames() As String = { "Sue", "Kim", _
"Alan", "Bill" }
Array.Sort(strNames)
Array.Reverse()
Sorting an Array, Ascending

• Arrays have a Sort method


• Arranges elements in ascending order (lowest to
highest)
• Sort intNumbers in this order: {1, 3, 6, 7, 12}
Dim intNumbers() As Integer = { 7, 12, 1, 6, 3 }
Array.Sort(intNumbers)
• Sort strNames in this order: {Alan, Bill, Kim, Sue}

Dim strNames() As String = { "Sue", "Kim", _


"Alan", "Bill" }
Array.Sort(strNames)
Dynamically Sizing an Array

ReDim [Preserve] Arrayname(UpperSubscript)

• ReDim is a new keyword


– Used to change the number of elements at run time
• If Preserve is specified, the existing contents of
the array are preserved
• Arrayname names the existing array
• UpperSubscript specifies the new highest
subscript value
• Can declare an array with no subscript and state
number of elements later with ReDim
Resizing Example
• Array sngScores declared with no elements
• User prompted for number of elements
• ReDim resizes array based on user input

Dim sngScores() As Single ' Declared with no elements


Dim intNumScores as Integer

' Obtain number of elements from the user


intNumScores = CInt(InputBox("Enter number of test scores"))
If intNumScores > 0 Then
ReDim sngScores(intNumScores - 1)
Else
MessageBox.Show("You must enter 1 or greater.")
End If

You might also like