double_trouble

nasledovanie

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