1 / 51

Arrays

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

kerri
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays • Group of variables that have a similar type • Integers • Strings • Doubles • An array of states, tax rates, social security numbers

  2. 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)

  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)

  4. Arrays • 1D Array: Dim arrayname(lower subscript TO upper subscript) as datatype *** lower subscript is set to 0

  5. Arrays - Examples Dim statearray(0 to 4) as string • Creates a string array that will contain state ids • Statearray(3) equals ‘DE’

  6. Arrays - Examples Dim montharray(0 to 11) as string • Creates a string array that will contain months in the year

  7. Arrays - Examples Dim numberarray(1 to 10) as Integer • Creates an integer array that will contain 10 numbers

  8. Arrays - Examples Dim ArrayVariable() as Integer • no set range of elements • Elements in this array are from 0 to n

  9. Arrays - Examples Dim myarray(5) as Integer For x = 0 to 4 myarray(x) = x+1 Form1.Print “array(“ & x & “) = “ & array(x) Next x

  10. Arrays - Examples Dim myarray(50) as string myarray(0) = “hello”

  11. Arrays - Examples Valuing an array: Default values of arrays are: 0 for numeric arrays spaces for string arrays false for boolean arrays

  12. Arrays - Examples Valuing an array: (element by element hard coded) Montharray(11) = “December” Numberarray(5) = 21

  13. Arrays - Examples Valuing an array: (use a loop) For x = 1 to 10 Myarray(x) = Val(Inputbox(“Enter a _ Number”)) Next x

  14. Arrays - Examples Dim myarray(5) as Integer For x = 1 to 5 Myarray(x) = x+1 Form1.Print “array(“ & x & “) = “ & array(x) Next x

  15. Arrays - Examples Getting array data: Txt1.text = montharray(5) ‘ displays ‘ MAY Text2.text = numberarray(5) ‘display 21

  16. 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...

  17. 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

  18. 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)

  19. 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

  20. Arrays - RANGES EXAMPLE: Dim mystring(10) as String For count = Lbound(mystring) TO ubound(mystring) mystring(count) = InputBox(“Enter a Name:”) Next count

  21. Arrays - RANGES EXAMPLE: RANGE Errors: Dim myarray(5) as integer Myarray(6) = 200 ‘ SUBSCRIPT ERROR

  22. Arrays - Unbounded • Unbounded/Dynamic Array: • Declare an array with no predefined size. It can vary in size during run time. Dim myarray() as Integer

  23. 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 !!!

  24. 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.

  25. 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

  26. Arrays - Redim • REDIM • Erase --- deallocates an array space if its dynamically allocated. Reinitializes the elements of a fixed sized array. Erase myarray

  27. Arrays - Redim • PRACTICE: Thompson (p. 9-6) Count the freequency of dice roll outcomes

  28. 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

  29. Arrays - 2 D Array • 2 Dimensional Array: • a matrix is a multidimensonal array used to store related information. • Checkerboard, tic-tac-toe board.

  30. 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)

  31. Arrays - 2 D Array • EXAMPLE: Dim a2d(3, 3) a2d(0, 0) = 1 a2d(2, 2) = 5 a2d(3, 3) = 9

  32. 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 Next

  33. Binary Search Dim lowi ,highi, midi as integer Lowi = Lbound(array) Highi = Ubound(array)

  34. 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

  35. Binary Search If boolfound = TRUE then “FOUND” else “NOT FOUND” End if

  36. 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.

  37. ArrayList • You can perform the following types of actions with an ArrayList: • Add • Count • Remove • Sort • Search

  38. 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)

  39. ArrayList • The following are examples on how to use each of these actions:

  40. ArrayList • Create the ArrayList: Dim myAL As ArrayList = New ArrayList

  41. ArrayList • Add elements to the ArrayList: Dim cc As Integer = 10 myAL.Add(cc) myAL.Add(20)

  42. 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

  43. ArrayList • Remove elements from the ArrayList: myAL.Remove(20)

  44. ArrayList • Check the list to see if a specific ob ject is in the ArrayList: TextBox1.Text = myAL.Contains(15)

  45. ArrayList • Return a specific element from the ArrayList: myAL.Item(0) = 40

  46. ArrayList • Sort elements of the ArrayList: myAL.Sort()

  47. ArrayList • Search for an element in the ArrayList: TextBox2.Text = myAL.BinarySearch(40)

  48. ArrayList • Check to see if 2 objects are equal: TextBox3.Text = myAL(0).Equals(myAL(0))

  49. ArrayList • Empty the ArrayList: myAL.Clear()

  50. 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...

More Related