Home

Introduction to Collections With Arrays

 

Introduction

A collection, also called a list, is many items of the same kind grouped into one entity. The collection can be based on simple numbers, basic symbols, dates, strings, times. The collection can also be made of object that each is made of internal values. This means that a collection can be made of such objects as houses, people, cars, countries, electronic devices. The primary rule is all items included in a list must be described using the same characteristics. Based on this, if the collection is made of cars, it should contain only cars, not cars and countries.

There are various types of collections or collections. An array-based list is one whose count is known in advance. For example, a array-based collection of 10 cars contains 10 cars, may-be less but not more.

Setting Up a Collection

To use a collection as an entity, you can create a class for it; this class would be used to add items in the collection, to remove items in the list, or to perform other necessary operations. You can start from a simple class as follows:

public ref class CCollection
{
};

After creating the class for a collection, the collection becomes an object and its class can be used like any other. This means that, before the collection, you can declare a variable for it here is an example:

using namespace System;

public ref class CCollection
{
};

int main()
{
    CCollection ^ list = gcnew CCollection;

    return 0;
}

The Number of Items in a Collection

For an array-based list, because you must specify the number of items that the list will contain, you can declare an array as a member variable. Here is an example:

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    static array<double> ^ Item = gcnew array<double>(20);

public:
    // Our default constructor, used to initialize the collection
    CCollection()
    {
    }
}

Although you can initialize a static array in a C++ class, remember that you can also use a constructor to initialize it. Here is an example:

using namespace System;

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
}

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 field 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:

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;


private:
    // This is the size of the collection
    int size;

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
    size = 0;
}

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:

using namespace System;

int MaxCount = 20;

public ref class CCollection
{
private:
    // This collection will be a list of decimal numbers
    array<double> ^ Item;

private:
    // This is the size of the collection
    int size;

public:
    // This represents the number of items in the collection
    property int Count
    {
        int get() { return size; }
    }

public:
    CCollection();
};

CCollection::CCollection()
{
    Item = gcnew array<double>(MaxCount);
    size = 0;
}

int main()
{
    CCollection ^ list = gcnew CCollection;

    Console::WriteLine(L"Number of Items: {0}", list->Count);

    return 0;
}

This would produce:

Number of Items: 0
Press any key to continue . . .
 

Home Copyright © 2007-2012 FunctionX Next