Small Basic #9: Arrays
Microsoft Small Basic supports arrays, which allow you to store items in temporary computer memory. Unlike the stack which allows you to add, read, and remove items in a specific order, arrays allow you to add, read, and remove items in random order. Also, while the stack only allows for one dimension, arrays allow for multiple dimensions. The following code demonstrates arrays:
' Add items to food array.
food[1] = "Apple"
food[2] = "Banana"
food[3] = "Coconut"
' What is in the food array?
For i = 1 To Array.GetItemCount(food)
TextWindow.WriteLine("food[" + i + "] contains " + food[i])
EndFor
TextWindow.WriteLine("-----")
TextWindow.WriteLine("food is an array: " + Array.IsArray(food))
' Does the fruit array exist yet?
TextWindow.WriteLine("fruit is an array: " + Array.IsArray(fruit))
TextWindow.WriteLine("food[2] exists: " + Array.ContainsIndex(food, 2))
TextWindow.WriteLine("food contains Coconut: " + Array.ContainsValue(food, "Coconut"))
TextWindow.WriteLine("food contains Grape: " + Array.ContainsValue(food, "Grape"))
TextWindow.WriteLine("food contains " + Array.GetItemCount(food) + " items.")
' Copy indices (not values) to fruit array.
fruit = Array.GetAllIndices(food)
TextWindow.WriteLine("-----")
' What is in the fruit array?
WhatIsInFruit()
TextWindow.WriteLine("-----")
' Note that the values are not copied into the fruit array.
TextWindow.WriteLine("fruit is an array now: " + Array.IsArray(fruit))
TextWindow.WriteLine("fruit[2] exists: " + Array.ContainsIndex(fruit, 2))
TextWindow.WriteLine("fruit contains Banana: " + Array.ContainsValue(fruit, "Banana"))
TextWindow.WriteLine("fruit contains Grapes: " + Array.ContainsValue(fruit, "Grapes"))
TextWindow.WriteLine("fruit contains " + Array.GetItemCount(fruit) + " items.")
fruit[1] = "Apple"
fruit[2] = "Banana"
fruit[3] = "Coconut"
fruit[4] = "Grape"
TextWindow.WriteLine("-----")
' What is in the fruit array now?
TextWindow.WriteLine("fruit contains Banana now: " + Array.ContainsValue(fruit, "Banana"))
TextWindow.WriteLine("fruit contains Grape now: " + Array.ContainsValue(fruit, "Grape"))
TextWindow.WriteLine("fruit now contains " + Array.GetItemCount(fruit) + " items.")
TextWindow.WriteLine("-----")
WhatIsInFruit()
Sub WhatIsInFruit
For i = 1 To Array.GetItemCount(fruit)
TextWindow.WriteLine("fruit[" + i + "] now contains " + fruit[i])
EndFor
EndSub
' Two-dimensional arrays.
fruitSalad[1][1] = "Apple"
fruitSalad[1][2] = "Banana"
fruitSalad[1][3] = "Coconut"
fruitSalad[2][1] = "Grape"
fruitSalad[2][2] = "Orange"
fruitSalad[2][3] = "Pear"
TextWindow.WriteLine("-----")
For i = 1 To 2
For j = 1 To 3
TextWindow.WriteLine("fruitSalad[" + i + "][" + j + "] contains " + fruitSalad[i][j])
EndFor
EndFor
mixedSalad["fruit"][1] = "Apple"
mixedSalad["fruit"][2] = "Banana"
mixedSalad["fruit"][3] = "Coconut"
mixedSalad["vegetables"][1] = "Lettuce"
mixedSalad["vegetables"][2] = "Tomato"
mixedSalad["vegetables"][3] = "Carrot"
TextWindow.WriteLine("-----")
For i = 1 To 3
TextWindow.WriteLine("mixedSalad[fruit][" + i + "] contains " + mixedSalad["fruit"][i])
EndFor
For i = 1 To 3
TextWindow.WriteLine("mixedSalad[vegetables][" + i + "] contains " + mixedSalad["vegetables"][i])
EndFor