Small Basic > Curriculum > Online > Lesson 3.2: Stacks and Arrays
In this lesson, you will learn how to:
Before we discuss the Array and Stack objects, let’s first understand when we might use either of these objects.
An array can have multiple dimensions, but a stack has only one dimension. You can directly access any element in an array, but you can access only the top element of a stack. In other words, you must go through all the elements of a stack to access its last element.
So far, you have learned about variables that store single values. Now, let’s learn about a special kind of variable that is called an array.
An array can store more than one value at the same time. If you want to store the names of five users, you can create five variables, or you can create just one variable to store all five names.
You use the indexing method to store multiple values in an array. For example, you can create an array called name as: name[1], name[2], name[3], name[4], and name[5]. Here, 1, 2, 3, 4, and 5 are the indices for the name array.
The name[1], name[2]… labels may appear to identify different variables, but they all represent just one variable!
Now, let’s discuss some operations of the Array object, such as IsArray, ContainsIndex, and ContainsValue.
If any of these operations succeeds, “TRUE” appears in the text window. Otherwise, “FALSE” appears.
Let’s see how we can use these operations in a program.
Subjects[1] = "English" Subjects[2] = "History" Subjects[3] = "Computers" Subjects[4] = "Science" Subjects[5] = "Math“ TextWindow.WriteLine("Subjects is an array: " + Array.IsArray(Subjects)) TextWindow.WriteLine("Subjects[4] is available: " + Array.ContainsIndex(Subjects, 4)) TextWindow.WriteLine("Math is available: " + Array.ContainsValue(Subjects, "Math")) Array.GetItemCount(Subjects)
In this example, the Subjects array stores the names of five subjects. You can verify whether Subjects is an array by using the IsArray operation. You can also verify whether the index Subjects[4] exists by using the ContainsIndex operation. You can verify whether the value “Math” exists in the Subjects array by using the ContainsValue operation.
The Array object also provides more useful operations, such as:
Look at this example to learn how to use these operations.
Employee["Name"] = "John" Employee["City"] = "New York" Employee["Email-ID"] = "John@example.com" Employee["Mobile"] = "06482624" Emplist = Array.GetAllIndices(Employee) For i = 1 To Array.GetItemCount(Employee) TextWindow.WriteLine(Emplist[i] + ":" + Employee[Emplist[i]]) EndFor
In this example, you don’t know the indices for the Employee array, so you use the GetAllIndices operation. Next, you use the GetItemCount operation in a For loop to list the information that is stored in the Employee array.
This is the output you will see:
You can use the GetAllIndices operation to get all the indices for the array, in the form of another array. This operation is especially useful when you don’t know the indices of an array. This operation displays an array along with all indices of the specified array. The index of the displayed array starts at 1.
You can use the GetItemCount operation to get the total number of items that are stored in the specified array. This operation displays the number of items in the specified array.
You can use the Stack object to store data the same way as you stack plates. This object works on the principle of last-in, first-out (LIFO).
For example, if you look down at a stack of plates, you can see only the top plate. To see the next plate, you must remove this top plate. You can’t see a plate in the middle of the stack until you remove the plates above it.
The Stack object consists of three operations:
Let’s explore each of these operations…
The Stack object stores data just as a stack of plates. Let’s look at some examples to understand how this object works.
The PushValue operation does not return anything.
The PopValue operation returns the value from the stack.
The GetCount operation returns the number of items in a specified stack.
Let’s write a program to better understand these operations.
container = "empty" MsgTotalPlates = "The number of plates in the container is " MsgRemovingPlates = "After taking away 8 plates, the total number of plates is " MsgTopPlate = "The top most plate in the container is " For i = 0 To 50 Stack.PushValue(container , "plate number " + i) EndFor TextWindow.WriteLine(MsgTotalPlates + Stack.GetCount(container)) For i = 0 To 8 Stack.PopValue(container) EndFor TextWindow.WriteLine(MsgRemovingPlates + Stack.GetCount(container)) TextWindow.WriteLine(MsgTopPlate + Stack.PopValue(container))
In this example, you use the PushValue operation to push 50 plates into an empty container. Then you take eight plates from the stack by using the PopValue operation. Now, you use the GetCount operation to get the number of plates that remain. You also display the value of the top plate.
Congratulations! Now you know how to:
By using the Array object, write a flight-reservation program that you can use to perform the following actions:
To see the answers to these questions, go to the Answer Key page.
Next Lesson
Fernando Lugão Veltem edited Revision 1. Comment: added toc