Ejemplo n.º 1
0
    public static void Main(string[] args) {
      ArrayList a = new ArrayList2(10);
      Contract.Assert(a.Count == 0);

      a.Add("apple");
      a.Add("cranberry");
      a.Add("banana");
      Contract.Assert(a.Count == 3);
      Contract.Assert(Equals(a[0], "apple"));
      Contract.Assert(Equals(a[1], "cranberry"));
      Contract.Assert(Equals(a[2], "banana"));

      Contract.Assert(a.IndexOf("apple") == 0);
      Contract.Assert(a.IndexOf("cranberry") == 1);
      Contract.Assert(a.IndexOf("banana") == 2);
      Contract.Assert(a.IndexOf("donut") == -1);

      a.Sort();
      Contract.Assert(a.Count == 3);
      Contract.Assert(Equals(a[0], "apple"));
      Contract.Assert(Equals(a[1], "banana"));
      Contract.Assert(Equals(a[2], "cranberry"));

      Contract.Assert(a.BinarySearch("apple") == 0);
      Contract.Assert(a.BinarySearch("banana") == 1);
      Contract.Assert(a.BinarySearch("cranberry") == 2);
      Contract.Assert(a.BinarySearch("donut") < 0);

      a.Reverse();
      Contract.Assert(a.Count == 3);
      Contract.Assert(Equals(a[2], "apple"));
      Contract.Assert(Equals(a[1], "banana"));
      Contract.Assert(Equals(a[0], "cranberry"));

      a.Remove("apple");
      Contract.Assert(a.Count == 2);
      Contract.Assert(Equals(a[0], "cranberry"));
      Contract.Assert(Equals(a[1], "banana"));

      a.RemoveAt(5);
      a.RemoveAt(0);
      Contract.Assert(a.Count == 1);
      Contract.Assert(Equals(a[0], "banana"));

      a.Clear();
      Contract.Assert(a.Count == 0);

      Contract.Assert(ArrayList.Repeat("carrot", 3).Count == 3);
    }