One of the most common operations in programming is the storage of large sets of data. There are databases, of course, which can store any type of data and preserve their structure as well, but not all applications use databases. If your application needs to store custom objects, such as the ones you designed in… [Continue Reading]
Storing Data in Collections
Sorting Arrays in Visual Basic 2008
Arrays are indexed sets of data, and this is how we’ve used them so far in this website. In this section, you will learn about additional members that make arrays extremely flexible. The System.Array class provides methods for sorting arrays, searching for an element, and more. In the past, programmers spent endless hours writing code… [Continue Reading]
VB.NET Searching Arrays & Binary Search Algorithm in Visual Basic 2008
Arrays can be searched in two ways: with the BinarySearch method, which works on sorted arrays and is extremely fast, and with the IndexOf (and LastIndexOf) methods, which work regardless of the order of the elements. All three methods search for an instance of an item and return its index, and they’re all reference methods…. [Continue Reading]
Different Methods of Array Operations in Visual Basic 2008
The Array class exposes additional methods, which are described briefly in this section. The Reverse method reverses the order of the elements in an array. The syntax of the Reverse method is the following: reversedArray = System.Array.Reverse(arrayName) The Reverse method can’t be applied to an array and reverse its elements. Instead, it returns a new… [Continue Reading]
The ArrayList Collection – Creating an ArrayList in Visual Basic 2008
The ArrayList collection allows you to maintain multiple elements, similar to an array; however, the ArrayList collection allows the insertion of elements anywhere in the collection, as well as the removal of any element. In other words, it’s a dynamic structure that can also grow automatically as you add/remove elements. Like an array, the ArrayList’s… [Continue Reading]
Adding and Removing ArrayList Items in Visual Basic 2008
To add a new item to an ArrayList, use the Add method, whose syntax is as follows: index = aList.Add(obj) aList is a properly declared ArrayList, and obj is the item you want to add to the ArrayList collection (it could be a number, a string, or an object). The Add method appends the specified… [Continue Reading]
Sorting ArrayLists in Visual Basic 2008
To sort the ArrayList, use the Sort method, which has three overloaded forms: aList.Sort() aList.Sort(comparer) aList.Sort(startIndex, endIndex, comparer) The ArrayList’s Sort method doesn’t require you to pass the name of the ArrayList to be sorted as an argument; unlike the Sort method of the Array class, this is an instance method and sorts the ArrayList… [Continue Reading]
Searching ArrayLists / Iterating an ArrayList in Visual Basic 2008
Like arrays, the ArrayList class exposes the IndexOf and LastIndexOf methods to search in an unsorted list and the BinarySearch method for sorted lists. The IndexOf and LastIndexOf methods accept as an argument the object to be located and return an index: Dim index As Integer = aList.IndexOf(object) Here, object is the item you’re searching…. [Continue Reading]
The HashTable Collection in Visual Basic 2008
As you saw, the ArrayList addresses most of the problems of the Array class, while it supports all the convenient array features. Yet, the ArrayList, like the Array, has a major drawback: You must access its items by an index value. Another collection, the HashTable collection, is similar to the ArrayList, but it allows you… [Continue Reading]
The WordFrequencies Example in Visual Basic 2008
In this section, you’ll develop an application that counts word frequencies in a text. The WordFrequencies application scans text files and counts the occurrences of each word in the text (download the project here). As you will see, the HashTable is the natural choice for storing this information because you want to access a word’s… [Continue Reading]