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 … [Read more...] about Storing Data in Collections
Storing Data in Collections
Sorting Arrays
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, … [Read more...] about Sorting Arrays
Searching Arrays & Binary Search Algorithm
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 … [Read more...] about Searching Arrays & Binary Search Algorithm
Different Methods of Array Operations
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: The Reverse method can't be applied to … [Read more...] about Different Methods of Array Operations
The ArrayList Collection – Creating an ArrayList
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 … [Read more...] about The ArrayList Collection – Creating an ArrayList
Adding and Removing ArrayList Items
To add a new item to an ArrayList, use the Add method, whose syntax is as follows: 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 … [Read more...] about Adding and Removing ArrayList Items
Sorting ArrayLists
To sort the ArrayList, use the Sort method, which has three overloaded forms: 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 … [Read more...] about Sorting ArrayLists
Searching ArrayLists / Iterating an ArrayList
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 … [Read more...] about Searching ArrayLists / Iterating an ArrayList
The HashTable Collection
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 … [Read more...] about The HashTable Collection
The WordFrequencies Example
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 … [Read more...] about The WordFrequencies Example
The SortedList Collection
The SortedList collection is a combination of the Array and HashTable classes. It maintains a list of items that can be accessed either with an index or with a key. When you access items by their indices, the SortedList behaves just like an … [Read more...] about The SortedList Collection
Working with Keys and Values
Let’s look now at a few methods for extracting keys and values. To find out the index of a value in the SortedList, use the IndexOfValue method, which accepts an object as an argument. If the object exists in the collection, it returns its index; if … [Read more...] about Working with Keys and Values
The IEnumerator and IComparer Interfaces
IEnumerator and IComparer are two classes that unlock some of the most powerful features of collections. The proper term for IEnumerator and IComparer is interface, a term I will describe shortly. Every class that implements the IEnumerator interface … [Read more...] about The IEnumerator and IComparer Interfaces
Enumerating Collections
All collections expose the GetEnumerator method. This method returns an object of the IEnumerator type, which allows you to iterate through the collection without having to know anything about its items, not even the count of the items. To retrieve … [Read more...] about Enumerating Collections
Custom Sorting – Sort Method
The Sort method allows you to sort collections, as long as the items are of the same base data type. If the items are objects, however, the collection doesn’t know how to sort them. If you want to sort objects, you must help the collection a little … [Read more...] about Custom Sorting – Sort Method
Generic Collections
All collections we examined so far were designed to store objects because they should accommodate all data types, built-in or custom. In most practical situations, however, we want to make sure that collections store elements of the same type, just … [Read more...] about Generic Collections