public class ArrayList<T>
{
T[] _innerArray = new T[4];
int _size = 0;
private void ResizeIfItNeeds()
{
if (_innerArray.Length == _size)
Array.Resize<T>(ref _innerArray, _size * 2);
}
private bool IsRangeValid(int position)
{
return ((position >= 0) && (position <= _size));
}
public int Count
{
get { return _size; }
}
public void InsertAt(T element, int position)
{
if (!IsRangeValid(position))
throw new ArgumentOutOfRangeException();
ResizeIfItNeeds();
Array.Copy(_innerArray, position, _innerArray, position + 1, _size – position);
_innerArray[position] = element;
_size++;
}
public T GetElementAt(int position)
{
if (!IsRangeValid(position))
throw new ArgumentOutOfRangeException();
return _innerArray[position];
}
public long RemoveAt(int position)
{
if (!IsRangeValid(position))
throw new ArgumentOutOfRangeException();
Array.Copy(_innerArray, position + 1, _innerArray, position, _size – position + 1);
return _size–;
}
public int GetPosition(T element, int startIndex, int length)
{
if (!IsRangeValid(startIndex + length) || !IsRangeValid(startIndex))
throw new ArgumentOutOfRangeException();
if (length == 1)
{
if (Object.Equals(element, _innerArray[startIndex]))
return startIndex;
else return -1;
}
int lastIndex = startIndex + length – 1;
int rangeSize = startIndex + length / 2;
int i = startIndex;
int j = 0;
for (; i <= rangeSize; i++, j++)
{
if (Object.Equals(_innerArray[i], element))
return i;
if (Object.Equals(_innerArray[lastIndex – j], element))
return lastIndex – j;
j++;
}
return -1;
}
public void Clear()
{
Array.Clear(_innerArray, 0, _innerArray.Length);
}
}