Arrays

1,022 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,022
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • CIS-166 Arrays 05/05/10
  • Arrays

    1. 1. Arrays <ul><li>List or series of values all referenced by the same name </li></ul><ul><li>Use an array to keep a series of variables for later processing such as </li></ul><ul><ul><li>Reordering </li></ul></ul><ul><ul><li>Calculating </li></ul></ul><ul><ul><li>Printing </li></ul></ul>
    2. 2. Array Terms <ul><li>Element </li></ul><ul><ul><li>Individual item in the array </li></ul></ul><ul><li>Index (or subscript) </li></ul><ul><ul><li>Zero based number used to reference the specific elements in the array </li></ul></ul><ul><ul><li>Must be an integer </li></ul></ul><ul><li>Boundaries </li></ul><ul><ul><li>Lower Subscript, 0 by default </li></ul></ul><ul><ul><li>Upper Subscript </li></ul></ul>
    3. 3. Simple Array Example strName Array (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James
    4. 4. General Form Dim Statement for Arrays Dim ArrayName ( UpperSubscript ) as Datatype Dim ArrayName ( ) as Datatype = {InitialValueList}
    5. 5. Dim Statement for Arrays Default Values <ul><li>Dim strName (3) as String </li></ul><ul><li>Results in an array of 4 elements: strName(0), strName(1), </li></ul><ul><li>strName(2), strName(3) </li></ul><ul><li>Dim decBalance (99) as Decimal </li></ul><ul><li>Results in an array of 100 elements: decBalance(0), . . . , decBalance(99) </li></ul>
    6. 6. Dim Statement for Arrays Assigned Values <ul><li>Dim strDept ( ) as String = {&quot;ACT&quot;, &quot;MKT&quot;, &quot;HR&quot;} </li></ul><ul><li>Results in an array with 3 elements, each with a value, strDept(0) is “ACT” </li></ul><ul><li>Dim intActCode ( ) as Integer = {10, 20, 30, 40} </li></ul><ul><li>Results in an array with 4 elements, each with a number stored </li></ul>
    7. 7. Dimensioning an Array <ul><li>When the DIM statement for the array is processed VB sets aside room for it in memory. </li></ul><ul><li>Ex: Dim strName (3) as String </li></ul><ul><ul><li>VB sets aside a memory location for 4 strings </li></ul></ul>strName (0) (1) (2) (3)
    8. 8. Referencing Array Elements <ul><li>Use the Index(es) of the Element </li></ul>Sam Smith Jill Creech Paul Fry Rich Wells strName(0) : &quot;Sam Smith&quot; strName(1) : &quot;Jill Creech&quot; strName(2) : &quot;Paul Fry&quot; strName(3) : &quot;Rich Wells&quot; strName(row) (0) (1) (2) (3)
    9. 9. Working with Arrays <ul><li>Use Loops to reference each element in the array </li></ul><ul><ul><li>For / Next </li></ul></ul><ul><ul><li>For Each / Next </li></ul></ul>
    10. 10. For Next Loop <ul><li>Assume strNames(9) already declared </li></ul><ul><li>Dim intCounter, intEnd as Integer </li></ul><ul><li>intEnd = strNames.GetUpperBound(0) </li></ul><ul><li>For intCounter = 0 to intEnd </li></ul><ul><ul><li>Console.Writeline(strNames(intCounter)) </li></ul></ul><ul><li>Next </li></ul>
    11. 11. For Each Loop <ul><li>Assume strNames(9) already declared </li></ul><ul><li>Dim strItem as String </li></ul><ul><li>For Each strItem in strNames </li></ul><ul><ul><li>Console.Writeline(strItem) </li></ul></ul><ul><li>Next </li></ul>
    12. 12. ReDim <ul><li>Use the ReDim keyword to change the length of the array </li></ul><ul><ul><li>Can increase or decrease the number of elements </li></ul></ul><ul><ul><li>Can keep existing values of elements using Preserve </li></ul></ul><ul><ul><li>ReDim Preserve strNames(19) </li></ul></ul>

    ×