Arrays Group of variables that have a similar type Integers Strings Doubles An array of states, tax rates, social security numbers
Arrays Arrays can be in one dimension or two dimensions (actually you can have up to 60 dimensions) 1D --- myarray(4) as string can contain: NJ(0), NY(1), DE(2), PA(3)
Arrays 2D --- myarray(3,3) can contain a tic-tac-toe board: X (0,0) O (0,1) X (0,2) O X O X (2,0) O (2,1) X (2,2)
Arrays 1D Array: Dim arrayname(lower subscript TO upper subscript) as datatype *** lower subscript is set to 0
Arrays - Examples Dim statearray(0 to 4) as string Creates a string array that will contain state ids Statearray(3) equals ‘DE’
Arrays - Examples Dim montharray(0 to 11) as string Creates a string array that will contain months in the year
Arrays - Examples Dim numberarray(1 to 10) as Integer Creates an integer array that will contain 10 numbers
Arrays - Examples no set range of elements Dim ArrayVariable() as Integer no set range of elements Elements in this array are from 0 to n
Arrays - Examples For x = 0 to 4 myarray(x) = x+1 Dim myarray(5) as Integer For x = 0 to 4 myarray(x) = x+1 Form1.Print “array(“ & x & “) = “ & array(x) Next x
Arrays - Examples Dim myarray(50) as string myarray(0) = “hello”
Arrays - Examples 0 for numeric arrays spaces for string arrays Valuing an array: Default values of arrays are: 0 for numeric arrays spaces for string arrays false for boolean arrays
Arrays - Examples Montharray(11) = “December” Numberarray(5) = 21 Valuing an array: (element by element hard coded) Montharray(11) = “December” Numberarray(5) = 21
Arrays - Examples For x = 1 to 10 Valuing an array: (use a loop) For x = 1 to 10 Myarray(x) = Val(Inputbox(“Enter a _ Number”)) Next x
Arrays - Examples Dim myarray(5) as Integer For x = 1 to 5 For x = 1 to 5 Myarray(x) = x+1 Form1.Print “array(“ & x & “) = “ & array(x) Next x
Arrays - Examples Getting array data: Txt1.text = montharray(5) ‘ displays ‘ MAY Text2.text = numberarray(5) ‘display 21
Arrays - Examples PRACTICE: ZAK p. 724 “The Presidents” Write an application that allows a user to guess the first 10 US Presidents. Create a string array of the presidents (form load) and randomly ask the user to guess the 1st, 2nd, etc… president. Continued...
Arrays - Examples PRACTICE: ZAK p. 724 “The Presidents” Have them select from a radio button control array of 15 unordered presidents. One they click on a “GUESS” button, examine their choice and match it to the string array. Let them know if they are right or wrong and keep track of their choices. Once they successfully selected a president, disable that choice and DO NOT allow that number to be used again
Arrays - RANGES Lbound --- identifies and returns the lowest index in the array (usually 0) Lbound(arrayname, DIM) ‘ DIM identifies the dimension of the array 1 is ‘ default EXAMPLE: Result = Lbound(myarray)
Arrays - RANGES Ubound --- identifies and returns the highest index in the array Ubound(arrayname, DIM) ‘ DIM identifies the dimension of the array 1 is default EXAMPLE: Result = Ubound(myarray) for x = 1 to Result … Next x
Arrays - RANGES EXAMPLE: Dim mystring(10) as String For count = Lbound(mystring) TO ubound(mystring) mystring(count) = InputBox(“Enter a Name:”) Next count
Arrays - RANGES EXAMPLE: RANGE Errors: Dim myarray(5) as integer Myarray(6) = 200 ‘ SUBSCRIPT ERROR
Arrays - Unbounded Unbounded/Dynamic Array: Declare an array with no predefined size. It can vary in size during run time. Dim myarray() as Integer
Arrays - Redim REDIM --- allocates/resets the size of an unbounded/dynamic array Redim myarray(10) ---- clears out the array and resizes it to the number of Elements ONLY USE THIS ON LOCALLY SCOPED ARRAYS !!!
Arrays - Redim REDIM To Preserve the existing array when redimming: Keeps existing values when redimming. Can only do when resetting the UPPERBOUNDRY of the array.
Arrays - Redim REDIM Redim Preserve myarray(10) preserves the existing contents of the array Redim Preserve myarray Ubound(myarray) + 1) dynamically adds 1 element to the array size
Arrays - Redim REDIM Erase --- deallocates an array space if its dynamically allocated. Reinitializes the elements of a fixed sized array. Erase myarray
Arrays - Redim PRACTICE: Thompson (p. 9-6) Count the freequency of dice roll outcomes
Arrays - Searching Searching and Array: Linear Iterate thru each array element looking for a specifice value, Example: ‘ find a given number in an integer array searchnum = 5 dim intarray(20) as integer do while (intarray(I) <> searchnum) AND (I < Ubound(intarray)) I = I + 1 loop
Arrays - 2 D Array 2 Dimensional Array: a matrix is a multidimensonal array used to store related information. Checkerboard, tic-tac-toe board.
Arrays - 2 D Array Dim my2darray(3,3) as Integer (X,Y) X = rows Y = columns R 0,0 0,1 0,2 0,3 O 1,0 1,1 1,2 1,3 W 2,0 2,1 2,2 2,3 S 3,0 3,1 3,2 3,3 COLUMNS (Y)
Arrays - 2 D Array EXAMPLE: Dim a2d(3, 3) a2d(0, 0) = 1 a2d(2, 2) = 5
Bubble Sort For x = Lbound(array) to Ubound(array) For y = Lbound(array) to Ubound(array)-1 if array(y) > array(y+1) then temp = array(y) array(y) = array(y+1) array(y+1) = temp end if Next
Binary Search Dim lowi ,highi, midi as integer Lowi = Lbound(array) Highi = Ubound(array)
Binary Search Do While Not boolfound AND lowi <= highi midi = Int((highi + lowi) / 2) if array(midi) = TARGETNUM then boolfound = TRUE elseif array(midi) > TARGETNUM then highi = midi -1 else lowi = mid + 1 end if loop
Binary Search If boolfound = TRUE then “FOUND” else “NOT FOUND” End if
ArrayList The ArrayList is a “class” that allows you to maintain an “array” of objects and have the ArrayList do all of the organizing of the data.
ArrayList You can perform the following types of actions with an ArrayList: Add Count Remove Sort Search
ArrayList You can perform the following types of actions with an ArrayList: Get an Item from the list (Item) Clear the list Determine if 2 elements are EQUAL (equal) Obtain the value at a specific index in the arraylist (IndexOf)
ArrayList The following are examples on how to use each of these actions:
ArrayList Create the ArrayList: Dim myAL As ArrayList = New ArrayList
ArrayList Add elements to the ArrayList: Dim cc As Integer = 10 myAL.Add(cc) myAL.Add(20)
ArrayList iterate thru all elements of the ArrayList (Count and Item): Dim z As Integer z = 0 Do While z < myAL.Count Button3.Text = Val(Button3.Text) + myAL.Item(z) z += 1 Loop
ArrayList Remove elements from the ArrayList: myAL.Remove(20)
ArrayList Check the list to see if a specific ob ject is in the ArrayList: TextBox1.Text = myAL.Contains(15)
ArrayList Return a specific element from the ArrayList: myAL.Item(0) = 40
ArrayList Sort elements of the ArrayList: myAL.Sort()
ArrayList Search for an element in the ArrayList: TextBox2.Text = myAL.BinarySearch(40)
ArrayList Check to see if 2 objects are equal: TextBox3.Text = myAL(0).Equals(myAL(0))
ArrayList Empty the ArrayList: myAL.Clear()
PRACTICE: Thompson (p. 9-26) Exercise 3 “Generated Numbers” PRACTICE: Thompson (p. 9-27) Exercise 5a “Generated Numbers” (5b advanced) PRACTICE: Thompson (p. 9-28) Exercise 8 “Duplicate Values” PRACTICE: Thompson (p. 9-29) Exercise 10 “Lockers” continued...
PRACTICE: Thompson (p. 9-30) Exercise 12 “Game Board 16” (12b advanced) PRACTICE: Thompson (p. 9-31 to 33) Exercises 13, 14, 15 and 16 (ADVANCED)