Advertisement
double_trouble

intList(ICollection)

Jun 20th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. namespace ConsoleApplication11
  8. {
  9.     class intList : System.Collections.ICollection
  10.     {
  11.         int[] M;
  12.         int count;
  13.         const int defaultCapacity = 0;
  14.  
  15.         public intList(int cap)
  16.         {
  17.             M = new int[cap];
  18.             count = 0;
  19.         }
  20.  
  21.        // int System.Collections.ICollection.Count
  22.         //{
  23.         //    get
  24.         //    {
  25.         //        return count;
  26.         //    }
  27.         //}
  28.  
  29.         public int Count
  30.         {
  31.             get { return count; }
  32.             set { value = count; }
  33.         }
  34.  
  35.         public int this[int ind]
  36.         {
  37.             get { return M[ind]; }
  38.             set { value = M[ind]; }
  39.         }
  40.  
  41.         bool System.Collections.ICollection.IsSynchronized
  42.         {
  43.             get
  44.             {
  45.                 return false;
  46.             }
  47.         }
  48.  
  49.         object System.Collections.ICollection.SyncRoot
  50.         {
  51.             get
  52.             {
  53.                 return this;
  54.             }
  55.         }
  56.  
  57.         void System.Collections.ICollection.CopyTo(Array array, int index)
  58.         {
  59.             intList l = new intList(array.Length);
  60.             foreach(int i in M)
  61.             {
  62.                 array.SetValue(i, index);
  63.                 index++;
  64.             }
  65.  
  66.         }
  67.  
  68.         IEnumerator IEnumerable.GetEnumerator()
  69.         {
  70.             for (int i = 0; i < count; i++)
  71.                 yield return M[i];
  72.         }
  73.  
  74.         public int Capacity
  75.         {
  76.             get { return M.Length; }
  77.             set
  78.             {
  79.                 if (value < count)
  80.                     value = count;
  81.                 if (value != M.Length)
  82.                 {
  83.                     int[] nm = new int[value];
  84.                     Array.Copy(M, nm, M.Length);
  85.                     M = nm;
  86.                 }
  87.             }
  88.         }
  89.  
  90.         public void Add(int item)
  91.         {
  92.             if (Capacity == count)
  93.                 Capacity = count * 2;
  94.             M[count] = item;
  95.             count++;
  96.         }
  97.  
  98.         public void AddRange(System.Collections.ICollection ar)
  99.         {
  100.             intList s = (intList)ar;
  101.             foreach(int item in s)
  102.             {
  103.                 this.Add(item);
  104.             }
  105.         }
  106.  
  107.         public void SetRange(int k, System.Collections.ICollection ar)
  108.         {
  109.             intList s = (intList)ar;
  110.             foreach(int item in s)
  111.             {
  112.                 if (k == Capacity)
  113.                     Add(item);
  114.                 else
  115.                     M[k] = item;
  116.                 k++;
  117.             }
  118.         }
  119.     }
  120.  
  121.     class Program
  122.     {
  123.         static void Main(string[] args)
  124.         {
  125.             intList IL = new intList(10);
  126.             IL.Add(1);
  127.             IL.Add(2);
  128.             IL.Add(3);
  129.             intList IL1 = new intList(10);
  130.             IL1.Add(4);
  131.             IL1.Add(5);
  132.             IL.AddRange(IL1);
  133.             IL.SetRange(0, IL1);
  134.             Console.WriteLine("count = {0}", IL.Count);
  135.             Console.WriteLine("Capacity = {0}", IL.Capacity);
  136.             for (int i =0; i < IL.Count; i++)
  137.             {
  138.                 Console.Write("{0,5}", IL[i]);
  139.             }
  140.             Console.WriteLine();
  141.             Console.ReadKey();
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement