|
Array-Based Lists
|
|
A list is a collection of items of the same kind. The
list can be based on numbers (natural or floating-point), text, dates,
times, or such values as long as the items are of the same type. Lists are
at the core of computer programming. They are used in databases,
spreadsheets, word processors and applications that are not primarily
list-oriented. We are going to create a list of
double-precision numbers. |
There are various ways to create a list. We will take
a look at the simplest approach, which is to use a normal array. With this
technique, you can start by creating a class. Here is an example:
|
Imports System
Module Exercise
Public Class ListOfNumbers
Public Sub New()
End Sub
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
End Sub
End Module
To start the list, you can declare an array as a member variable:
|
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Public Sub New()
End Sub
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
End Sub
End Module
A primary accessory you need when using a list is a
count of the number of items in the list when they are added or deleted. This
accessory is primarily a private member variable as a simple natural
number. When the class is declared, this member variable
should be set to 0 to indicate that the list is primarily empty: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
End Sub
End Module
Since the size member variable was declared private,
if you plan to get the count of items in the list from outside the class,
you can provide a property to take care of this. This can simply be done as
follows: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
End Sub
End Module
Current Number of Items: 0
|
Creating a list consists of adding items to it. Items
are usually added one at a time. The easiest way to do this is to add an
item at the end of the existing list. This is the easiest of your
operations.
To add an item to the list, you first check whether
the list is already full. A list is full if its count of items is equal to
or
higher than the maximum number you had set. If the list is not empty, you
can add an item at the end and increase the count by one. Here is how this
can be done: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
' Adds a new item to the list if the list is not full
' Increases the number of items in the list
' Returns true if the item was added, otherwise returns false
Public Function Add(ByVal itm As Double) As Boolean
' Make sure the list is not yet full
If size < 20 Then
' Since the list is not full, add the "item" at the end
Item(size) = itm
' Increase the count and return the new position
size = size + 1
' Indicate that the item was successfully added
Return True
End If
' If the item was not added, return false
Return False
End Function
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
End Sub
End Module
Once you have a means of adding items to the list, you
can effectively create a list of items. Here is an example: |
|
|
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
' Adds a new item to the list if the list is not full
' Increases the number of items in the list
' Returns true if the item was added, otherwise returns false
Public Function Add(ByVal itm As Double) As Boolean
' Make sure the list is not yet full
If size < 20 Then
' Since the list is not full, add the "item" at the end
Item(size) = itm
' Increase the count and return the new position
size = size + 1
' Indicate that the item was successfully added
Return True
End If
' If the item was not added, return false
Return False
End Function
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
lstNumbers.Add(224.52)
lstNumbers.Add(60.48)
lstNumbers.Add(1250.64)
lstNumbers.Add(8.86)
lstNumbers.Add(1005.36)
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
End Sub
End Module
After adding items to a list, you can retrieve them to
do what you intended the list for. To retrieve an item, you can locate an
item by its position, the same way you would do for an array. Having this
index, you can check if the position specified is negative or higher than
the current count of items. If it is, there is nothing much to do since
the index would be wrong. If the index is in the right range, you can
retrieve its corresponding item. The method to do this can be implemented
as follows: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
' Adds a new item to the list if the list is not full
' Increases the number of items in the list
' Returns true if the item was added, otherwise returns false
Public Function Add(ByVal itm As Double) As Boolean
' Make sure the list is not yet full
If size < 20 Then
' Since the list is not full, add the "item" at the end
Item(size) = itm
' Increase the count and return the new position
size = size + 1
' Indicate that the item was successfully added
Return True
End If
' If the item was not added, return false
Return False
End Function
' Retrieves an item from the list based on the specified index
Public Function Retrieve(ByVal pos As Integer) As Double
' Make sure the index is in the range
If pos >= 0 And pos <= size Then
Return Item(pos)
End If
' If the index was wrong, return 0
Return 0.0
End Function
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
lstNumbers.Add(224.52)
lstNumbers.Add(60.48)
lstNumbers.Add(1250.64)
lstNumbers.Add(8.86)
lstNumbers.Add(1005.36)
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
End Sub
End Module
You can then call such a method to locate an item and
use its value. Here is an example: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
' Adds a new item to the list if the list is not full
' Increases the number of items in the list
' Returns true if the item was added, otherwise returns false
Public Function Add(ByVal itm As Double) As Boolean
' Make sure the list is not yet full
If size < 20 Then
' Since the list is not full, add the "item" at the end
Item(size) = itm
' Increase the count and return the new position
size = size + 1
' Indicate that the item was successfully added
Return True
End If
' If the item was not added, return false
Return False
End Function
' Retrieves an item from the list based on the specified index
Public Function Retrieve(ByVal pos As Integer) As Double
' Make sure the index is in the range
If pos >= 0 And pos <= size Then
Return Item(pos)
End If
' If the index was wrong, return 0
Return 0.0
End Function
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
lstNumbers.Add(224.52)
lstNumbers.Add(60.48)
lstNumbers.Add(1250.64)
lstNumbers.Add(8.86)
lstNumbers.Add(1005.36)
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
For i As Integer = 0 To lstNumbers.Count - 1
Console.WriteLine("Item {0}: {1}", i + 1, lstNumbers.Retrieve(i))
Next
Console.WriteLine()
End Sub
End Module
Complex Operations on a List |
|
Inserting a new item in the list allows you to add one
at a position of your choice. To insert a new item in the list, you must
provide the new item and the desired position. Before performing this
operation, you must first check two things. First, the list must not be
empty. Second, the specified position must be in the allowed range.
The method can be implemented as follows: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
' Adds a new item to the list if the list is not full
' Increases the number of items in the list
' Returns true if the item was added, otherwise returns false
Public Function Add(ByVal itm As Double) As Boolean
' Make sure the list is not yet full
If size < 20 Then
' Since the list is not full, add the "item" at the end
Item(size) = itm
' Increase the count and return the new position
size = size + 1
' Indicate that the item was successfully added
Return True
End If
' If the item was not added, return false
Return False
End Function
' Retrieves an item from the list based on the specified index
Public Function Retrieve(ByVal pos As Integer) As Double
' Make sure the index is in the range
If pos >= 0 And pos <= size Then
Return Item(pos)
End If
' If the index was wrong, return 0
Return 0.0
End Function
' Before performing this operation, check that
' 1. The list is not full
' 2. The specified position is in an allowable range
' Inserts a new item at a specified position in the list
' After the new item is inserted, the count is increased
Public Function Insert(ByVal itm As Double, ByVal pos As Integer) As Boolean
' Check that the item can be added to the list
If size < 20 And pos >= 0 And pos <= size Then
' Since there is room,
' starting from the end of the list to the new position,
' push each item to the next or up
' to create room for the new item
For i As Integer = size To pos - 1 Step -1
Item(i + 1) = Item(i)
Next
' Now that we have room, put the new item in the position created
Item(pos) = itm
' Since we have added a new item, increase the count
size = size + 1
' Indicate that the operation was successful
Return True
End If
' Since the item could not be added, return false
Return False
End Function
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
lstNumbers.Add(224.52)
lstNumbers.Insert(60.48, 1)
lstNumbers.Add(1250.64)
lstNumbers.Add(8.86)
lstNumbers.Insert(1005.36, 1)
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
For i As Integer = 0 To lstNumbers.Count - 1
Console.WriteLine("Item {0}: {1}", i + 1, lstNumbers.Retrieve(i))
Next
Console.WriteLine()
End Sub
End Module
This would produce:
Number of Items: 5
Item 1: 224.52
Item 2: 1005.36
Item 3: 60.48
Item 4: 1250.64
Item 5: 8.86
Another operation you can perform on a list consists
of deleting an item. This is also referred to as removing the item. To
delete an item from the list, you can provide its position. Before
performing the operation, you can first check that the specified position
is valid. The method to perform this operation can be implemented as
follows: |
Imports System
Module Exercise
Public Class ListOfNumbers
Private Item(20) As Double
Private size As Integer
Public Sub New()
size = 0
End Sub
' Adds a new item to the list if the list is not full
' Increases the number of items in the list
' Returns true if the item was added, otherwise returns false
Public Function Add(ByVal itm As Double) As Boolean
' Make sure the list is not yet full
If size < 20 Then
' Since the list is not full, add the "item" at the end
Item(size) = itm
' Increase the count and return the new position
size = size + 1
' Indicate that the item was successfully added
Return True
End If
' If the item was not added, return false
Return False
End Function
' Retrieves an item from the list based on the specified index
Public Function Retrieve(ByVal pos As Integer) As Double
' Make sure the index is in the range
If pos >= 0 And pos <= size Then
Return Item(pos)
End If
' If the index was wrong, return 0
Return 0.0
End Function
' Before performing this operation, check that
' 1. The list is not full
' 2. The specified position is in an allowable range
' Inserts a new item at a specified position in the list
' After the new item is inserted, the count is increased
Public Function Insert(ByVal itm As Double, ByVal pos As Integer) As Boolean
' Check that the item can be added to the list
If size < 20 And pos >= 0 And pos <= size Then
' Since there is room,
' starting from the end of the list to the new position,
' push each item to the next or up
' to create room for the new item
For i As Integer = size To pos - 1 Step -1
Item(i + 1) = Item(i)
Next
' Now that we have room, put the new item in the position created
Item(pos) = itm
' Since we have added a new item, increase the count
size = size + 1
' Indicate that the operation was successful
Return True
End If
' Since the item could not be added, return false
Return False
End Function
' Removes an item from the list
' First check that the specified position is valid
' Deletes the item at that position and decreases the count
Public Function Delete(ByVal pos As Integer) As Boolean
' Make sure the position specified is in the range
If pos >= 0 And pos <= size Then
' Since there is room, starting at the specified position,
' Replace each item by the next
For i As Integer = pos To size
Item(i) = Item(i + 1)
Next
' Since an item has been removed, decrease the count
size = size - 1
' Indicate that the operation was successful
Return True
End If
' Since the position was out of range, return false
Return False
End Function
Public ReadOnly Property Count()
Get
Return size
End Get
End Property
End Class
Sub Main()
Dim lstNumbers As ListOfNumbers = New ListOfNumbers
lstNumbers.Add(224.52)
lstNumbers.Insert(60.48, 1)
lstNumbers.Add(1250.64)
lstNumbers.Add(8.86)
lstNumbers.Insert(1005.36, 1)
Console.WriteLine("Number of Items: {0}", lstNumbers.Count)
For i As Integer = 0 To lstNumbers.Count - 1
Console.WriteLine("Item {0}: {1}", i + 1, lstNumbers.Retrieve(i))
Next
lstNumbers.Delete(2)
Console.WriteLine(vbCrLf & "After deleting the third item...")
For i As Integer = 0 To lstNumbers.Count - 1
Console.WriteLine("Item {0}: {1}", i + 1, lstNumbers.Retrieve(i))
Next
Console.WriteLine()
End Sub
End Module
Number of Items: 5
Item 1: 224.52
Item 2: 1005.36
Item 3: 60.48
Item 4: 1250.64
Item 5: 8.86
After deleting the third item...
Item 1: 224.52
Item 2: 1005.36
Item 3: 1250.64
Item 4: 8.86