using System.Collections.Generic; namespace Collections { /// /// 配列リスト。 /// /// 要素の型 public class ArrayList : IEnumerable { #region フィールド T[] data; int count; #endregion #region 初期化 public ArrayList() : this(256) {} /// /// 初期最大容量を指定して初期化。 /// /// 初期載大容量 public ArrayList(int capacity) { this.data = new T[capacity]; this.count = 0; } #endregion #region プロパティ /// /// 格納されている要素数。 /// public int Count { get { return this.count; } } /// /// i 番目の要素を読み書き。 /// /// 読み書き位置 /// 読み出した要素 public T this[int i] { get { return this.data[i]; } set { this.data[i] = value; } } #endregion #region 挿入・削除 /// /// 配列を確保しなおす。 /// /// /// 配列長は2倍ずつ拡張していきます。 /// void Extend() { T[] data = new T[this.data.Length * 2]; for (int i = 0; i < this.data.Length; ++i) data[i] = this.data[i]; this.data = data; } /// /// i 番目の位置に新しい要素を追加。 /// /// 追加位置 /// 追加する要素 public void Insert(int i, T elem) { if (this.count >= this.data.Length) this.Extend(); for (int n = this.count; n > i; --n) { this.data[n] = this.data[n - 1]; } this.data[i] = elem; ++this.count; } /// /// 末尾に新しい要素を追加。 /// /// 追加する要素 public void InsertLast(T elem) { if (this.count >= this.data.Length) this.Extend(); this.data[this.count] = elem; ++this.count; } /// /// i 番目の要素を削除。 /// /// 削除位置 public void Erase(int i) { for (int n = i; n < this.count - 1; ++n) { this.data[n] = this.data[n + 1]; } --this.count; } /// /// 末尾の要素を削除。 /// public void EraseLast() { --this.count; } #endregion #region IEnumerable メンバ public IEnumerator GetEnumerator() { for (int i = 0; i < this.count; ++i) yield return this.data[i]; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } #endregion } }