This task is about arrays.

For hashes or associative arrays, please see Creating an Associative Array.

For a definition and in-depth discussion of what an array is, see Array.



Task

Show basic array syntax in your language.

Basically, create an array, assign a value to it, and retrieve an element   (if available, show both fixed-length arrays and
dynamic arrays, pushing a value into it).

Please discuss at Village Pump:   Arrays.

Please merge code in from these obsolete tasks:

   Creating an Array
   Assigning Values to an Array
   Retrieving an Element of an Array


Related tasks

   Collections
   Creating an Associative Array
   Two-dimensional array (runtime)


' compile with: FBC -s console. 
' compile with: FBC -s console -exx to have boundary checks.
 
Dim As Integer a(5)  ' from s(0) to s(5)
Dim As Integer num = 1
Dim As String s(-num To num) ' s1(-1), s1(0) and s1(1)
 
Static As UByte c(5) ' create a  array with 6 elements (0 to 5)
 
'dimension array and initializing it with Data
Dim d(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}
Print "  The first dimension has a lower bound of"; LBound(d);_
                           " and a upper bound of"; UBound(d)
Print " The second dimension has a lower bound of"; LBound(d,2);_
                           " and a upper bound of"; UBound(d,2)
Print : Print
 
Dim Shared As UByte u(0 To 3) ' make a shared array of UByte with 4 elements
 
Dim As UInteger pow() ' make a variable length array
' you must Dim the array before you can use ReDim
ReDim pow(num) ' pow now has 1 element
pow(num) = 10  ' lets fill it with 10 and print it
Print " The value of pow(num) = "; pow(num)
 
ReDim pow(10)  ' make pow a 10 element array
Print
Print " Pow now has"; UBound(pow) - LBound(pow) +1; " elements"
' the value of pow(num) is gone now
Print " The value of pow(num) = "; pow(num); ", should be 0"
 
Print
For i As Integer = LBound(pow) To UBound(pow)
    pow(i) = i * i
    Print pow(i),
Next
Print:Print
 
ReDim Preserve pow(3 To 7)
' the first five elements will be preserved, not elements 3 to 7
Print
Print " The lower bound is now"; LBound(pow);_
      " and the upper bound is"; UBound(pow)
Print " Pow now has"; UBound(pow) - LBound(pow) +1; " elements"
Print
For i As Integer = LBound(pow) To UBound(pow)
    Print pow(i),
Next
Print : Print
 
'erase the variable length array
Erase pow
Print " The lower bound is now"; LBound(pow);_
     " and the upper bound is "; UBound(pow)
Print " If the lower bound is 0 and the upper bound is -1 it means,"
Print " that the array has no elements, it's completely removed"
Print : Print
 
'erase the fixed length array
Print " Display the contents of the array d"
For i As Integer = 1 To 2 : For j As Integer = 1 To 5
    Print d(i,j);" ";
Next : Next : Print : Print 
 
Erase d
Print " We have erased array d"
Print "  The first dimension has a lower bound of"; LBound(d);_
                           " and a upper bound of"; UBound(d)
Print " The second dimension has a lower bound of"; LBound(d,2);_
                           " and a upper bound of"; UBound(d,2)
Print
For i As Integer = 1 To 2 : For j As Integer = 1 To 5
    Print d(i,j);" ";
Next : Next
Print
Print " The elements self are left untouched but there content is set to 0"
 
' empty keyboard buffer 
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End