Advertisement
double_trouble

DataTimeList

Jun 19th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 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 govno_sobach_e
  8. {
  9.     class DateTimeList
  10.     {
  11.         DateTime[] M;
  12.         int count = 0;
  13.  
  14.         public DateTimeList(int cap)
  15.         {
  16.             M = new DateTime[cap];
  17.         }
  18.  
  19.         public DateTime this[int ind]
  20.         {
  21.             get { return M[ind]; }
  22.             set { M[ind] = value; }
  23.         }
  24.  
  25.         public int Count
  26.         {
  27.             get { return count; }
  28.             set { count = value; }
  29.         }
  30.  
  31.         public int Capacity
  32.         {
  33.             get { return M.Length; }
  34.             set
  35.             {
  36.                 if (value < count)
  37.                     value = count;
  38.                 if (value != M.Length)
  39.                 {
  40.                     DateTime[] nm = new DateTime[value];
  41.                     Array.Copy(M, nm, count);
  42.                     M = nm;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         public void Add(System.DateTime item)
  48.         {
  49.             if (count == Capacity)
  50.                 Capacity = count * 2;
  51.             M[count] = item;
  52.             count++;
  53.         }
  54.  
  55.         public void Insert(int ind, System.DateTime item)
  56.         {
  57.             this.Add(M[count - 1]);
  58.             for (int i = count - 1; i > ind; i--)
  59.                 M[i] = M[i - 1];
  60.             M[ind] = item;
  61.         }
  62.  
  63.         public int IndexOf(System.DateTime value, int startIndex, int count)
  64.         {
  65.             int i = startIndex;
  66.             while(i < startIndex + count)
  67.             {
  68.                 if (M[i] == value)
  69.                     return i;
  70.             }
  71.             return -1;
  72.         }
  73.  
  74.         public void CopyTo(System.DateTime[] myTargetArray)
  75.         {
  76.  
  77.             for (int i = 0; i < myTargetArray.Length; i++)
  78.                 this.Add(myTargetArray[i]);
  79.         }
  80.     }
  81.  
  82.     class Program
  83.     {
  84.         static void Main(string[] args)
  85.         {
  86.             DateTimeList myc = new DateTimeList(6);
  87.             myc.Add(new DateTime(2015, 11, 04));
  88.             myc.Add(new DateTime(2015, 11, 04));
  89.             myc.Add(new DateTime(2015, 11, 04));
  90.             DateTime Val1 = new DateTime(2014, 11, 23);
  91.             DateTime Val2 = new DateTime(2015, 11, 04);
  92.             myc.Insert(1, Val1);
  93.             DateTime[] Dat = new DateTime[4] { new DateTime(2015, 12, 04), new DateTime(2011, 11, 04), new DateTime(2015, 08, 04), new DateTime(2012, 11, 04) };
  94.  
  95.  
  96.             for (int i = 0; i < myc.Count; i++)
  97.                 Console.WriteLine(myc[i]);
  98.             // foreach(DateTime x in myc)
  99.             // Console.WriteLine(x);
  100.             Console.WriteLine(myc.IndexOf(Val2, 2, 3));
  101.             Console.WriteLine("count={0}", myc.Count);
  102.             Console.WriteLine("Capacity={0}", myc.Capacity);
  103.             myc.CopyTo(Dat);
  104.             for (int i = 0; i < myc.Count; i++)
  105.                 Console.WriteLine(" {0}", myc[i]);
  106.             Console.ReadKey();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement