Home   Cover Cover Cover Cover
 

Klassen

../../solutions/2/GrowableArray.cs
using System;

class GrowableArray {
  object[] a;
  
  public GrowableArray(int size) {
    a = new object[size];
  }
  
  public GrowableArray() : this(8) {}
  
  void Grow() {
    object[] b = new object[2 * a.Length];
    Array.Copy(a, b, a.Length);
    a = b;
  }
  
  public object this[int i] {
    set {
      if (i >= a.Length) Grow();
      a[i] = value;
    }
    get {
      if (i >= a.Length) Grow();
      return a[i];
    }
  }
}

class Test {

  public static void Main() {
    GrowableArray a = new GrowableArray(2);
    a[0] = 0;
    a[1] = 1;
    a[2] = 2;
    for (int i = 0; i < 3; i++) Console.Write(" " + a[i]);
  }
}