Tag Archives: .NET4.0

ArrayList collection in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayList
{
    class arrayList<T>
    {

        private T[] _ar;

        private int originalSize;
        private int capacity; // size of the ArrayList
        private int elements; // elements in the ArrayList

        
        public arrayList()
        {
            originalSize = 10;
            capacity= 10;
            elements = 0;
            _ar = new T[capacity]; // use a default size of 10
            
        }

        public arrayList(int size)
        {
            originalSize = size;
            capacity = originalSize;
            elements = 0;
            _ar = new T[capacity]; // use a default size of 10
        }

        #region getters

        public int getCapacity()
        {
            return this.capacity;
        }

        public int countElements()
        {
            return this.elements;
        }

        #endregion

        public void Add(T item) // returns boolean
        {
            checkArray();

            _ar[elements] = item;
            elements++;   
        
        }

        public void Add(IEnumerable<T> collection)
        {
            foreach (T _T in collection)
            {
                this.Add(_T);
            }
        }



        public void Clear()
        {
            for (int i = 0; i < elements; i++)
                _ar[i] = default(T);
        }

        public bool Contains(T item)
        {
            bool Contains = false;

            for (int i = 0; i < elements; i++)
            {
                if (_ar[i].ToString() == item.ToString())
                {
                    Contains = true;
                    break;
                }
            }

            return Contains;
        }

        private void checkArray()
        {
            if (elements == capacity)
            {
                T[] _ar2 = new T[originalSize + capacity];
                for(int i = 0; i < _ar.Length; i++)
                {
                    _ar2[i] = _ar[i];
                }

                capacity = _ar2.Length;
                _ar = _ar2;
                // nieuwe array
            }
        }

        public void RemoveAll()
        {
            _ar = new T[capacity];
            elements = 0;
        }

        public void Remove(int Index)
        {
            // remove the item at the specified Index

            for (int i = Index; i < elements - 1; i++)
            {
                _ar[i] = _ar[i + 1];
            }

            _ar[elements - 1] = default(T);
            elements--;
        }

        public int getIndex(T item)
        {
            //returns the index of the first occurance of this Item.
            int index = -1;
            for (int i = 0; i < elements; i++)
            {
                if (_ar[i].ToString() == item.ToString())
                {
                    index = i;
                    break;
                }
            }

            //returns -1 if the index isn't found
            return index;

        }

        public bool set(int index, T item)
        {
            // replaces the item at the current index with the new item
            // returns true if the Set was a success.
            bool boolean = false;
            try
            {
                _ar[index] = item;
                boolean = true;
            }
            catch (Exception ex)
            {
                boolean = false;
            }

            return boolean;
        
        }

        public void Randomize()
        {
            
        }

        
        
    }
}