Home

Array-Based Lists

     

Introduction

An array-based list is a collection of items where each item can be located using its index.

Here is an example of an array-based list in C#:

using System;

public class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion

    #region Operations on the collection
    // 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 bool Add(double item)
    {
        // Make sure the list is not yet full
        if (size < MaxCount)
        {
            // Since the list is not full, add the "item" at the end
            this.Item[this.size] = item;
            // Increase the count and return the new position
            this.size++;

            // Indicate that the item was successfully added
            return true;
        }

        // If the item was not added, return false;
        return false;
    }

    // Retrieves an item from the list based on the specified index
    public double Retrieve(int pos)
    {
        // Make sure the index is in the range
        if (pos >= 0 && pos <= size)
            return this.Item[pos];

        // If the index was wrong, return 0
        return 0;
    }

    // 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 bool Insert(double itm, int pos)
    {
        // Check that the item can be added to the list
        if (size < 20 && pos >= 0 && pos <= size)
        {
            // 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 (int i = size; i > pos - 1; i--)
                this.Item[i + 1] = this.Item[i];

            // Now that we have room, put the new item in the position created
            this.Item[pos] = itm;

            // Since we have added a new item, increase the count
            this.size++;

            // Indicate that the operation was successful
            return true;
        }

        // Since the item could not be added, return false
        return false;
    }

    // Removes an item from the list
    // First check that the specified position is valid
    //-- Delete the item at that position and decrease the count --//
    public bool Delete(int pos)
    {
        // Make sure the position specified is in the range
        if (pos >= 0 && pos <= size)
        {
            // Since there is room, starting at the specified position,
            // Replace each item by the next
            for (int i = pos; i < this.size; i++)
                this.Item[i] = this.Item[i + 1];

            // Since an item has been removed, decrease the count
            this.size--;

            // Indicate that the operation was successful
            return true;
        }

        // Since the position was out of range, return false
        return false;
    }
    #endregion
}

public class Exercise
{
    static int Main(string[] args)
    {
        var list = new Collection();

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

        list.Add(224.52);
        list.Add(60.48);
        list.Add(1250.64);
        list.Add(8.86);
        list.Add(1005.36);


        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number of Items: {0}\n", list.Count);

        list.Insert(-707.16, 2);
        list.Insert(-369952.274, 4);

        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number of Items: {0}\n", list.Count);

        list.Delete(5);
        list.Delete(3);

        for (var i = 0; i < list.Count; i++)
            Console.WriteLine("Item {0}: {1}", i + 1, list.Retrieve(i));
        Console.WriteLine("Number of Items: {0}\n", list.Count);

        return 0;
    }
}
 

This would produce:

Number of Items: 0
Item 1: 224.52
Item 2: 60.48
Item 3: 1250.64
Item 4: 8.86
Item 5: 1005.36
Number of Items: 5

Item 1: 224.52
Item 2: 60.48
Item 3: -707.16
Item 4: 1250.64
Item 5: -369952.274
Item 6: 8.86
Item 7: 1005.36
Number of Items: 7

Item 1: 224.52
Item 2: 60.48
Item 3: -707.16
Item 4: -369952.274
Item 5: 1005.36
Number of Items: 5

Press any key to continue . . .
 
 

Home Copyright © 2009-2011 C# Key