fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5.  
  6. namespace Generic_Tab
  7. {
  8. class GenericArray<T> : IEnumerable<T>
  9. {
  10. //------- Hashtable -------------------------------------------------
  11. private Hashtable array; //Deklaracja tablicy typu Hashtable
  12. private uint size;
  13.  
  14. public GenericArray(uint size)
  15. {
  16. array = new Hashtable(); //Utworzenie tablicy konstruktorem
  17. this.size = size;
  18. }
  19. public T this[uint i]
  20. {
  21. get
  22. {
  23. return array.ContainsKey(i) ? (T)array[i] : default(T);
  24. }
  25. set
  26. {
  27. if (i >= size || i < 0)
  28. throw new IndexOutOfRangeException();
  29. if (!value.Equals(default(T)))
  30. {
  31. array[i] = value;
  32. }
  33. else if (array.Contains(i))
  34. {
  35. array.Remove(i);
  36. }
  37. }
  38. }
  39. public IEnumerator<T> GetEnumerator()
  40. {
  41. foreach (DictionaryEntry entry in array)
  42. {
  43. yield return (T)entry.Value ;
  44. }
  45. }
  46. IEnumerator IEnumerable.GetEnumerator()
  47. {
  48. foreach (DictionaryEntry entry in array)
  49. {
  50. yield return (T)entry.Value;
  51. }
  52. }
  53.  
  54. //-------- Dictionary ---------------------------------------------------
  55. /*private Dictionary<uint, T> array;
  56.   private uint size;
  57.  
  58.   public GenericArray(uint size)
  59.   {
  60.   array = new Dictionary<uint, T>(); //Utworzenie tablicy konstruktorem
  61.   this.size = size;
  62.   }
  63.  
  64.   public T this[uint index]
  65.   {
  66.   get
  67.   {
  68.   return array.ContainsKey(index) ? (T)array[index] : default(T);
  69.   }
  70.   set
  71.   {
  72.   if (index >= size || index < 0)
  73.   throw new IndexOutOfRangeException();
  74.   if (!value.Equals(default(T)))
  75.   {
  76.   array[index] = value;
  77.   }
  78.   else if (array.ContainsKey(index))
  79.   {
  80.   array.Remove(index);
  81.   }
  82.   }
  83.   }
  84.  
  85.   public IEnumerator<T> GetEnumerator()
  86.   {
  87.   foreach (KeyValuePair<uint, T> keyValuePair in array)
  88.   {
  89.   yield return keyValuePair.Value;
  90.   }
  91.   }
  92.   IEnumerator IEnumerable.GetEnumerator()
  93.   {
  94.   foreach (KeyValuePair<uint, T> keyValuePair in array)
  95.   {
  96.   yield return keyValuePair.Value;
  97.   }
  98.   }*/
  99. //----------------------------------------------------------------
  100. }
  101.  
  102. class Test
  103. {
  104. //Przykład uzycia i obsługi tablicy generycznej
  105. static void Main(string[] args)
  106. {
  107. uint size = 6;
  108. //--------------------------------------------------
  109. GenericArray<int> intArray = new GenericArray<int>(size);
  110.  
  111. for (uint c = 0; c < 4; c++) //Wstawienie wartości do tablicy
  112. {
  113. intArray[c] = (int)(c*c);
  114. }
  115. intArray[4] = 0;
  116. intArray[5] = 7;
  117.  
  118. Console.Write("\nTablica int = [ "); //Pobranie wartości do wyświetlenia
  119. foreach (int s in intArray)
  120. {
  121. Console.Write(s + " ");
  122. }
  123. Console.Write("]\n");
  124.  
  125. //--------------------------------------------------
  126. GenericArray<char> charArray = new GenericArray<char>(size);
  127.  
  128. for (uint c = 0; c < size; c++) //Wstawienie charów do tablicy
  129. {
  130. charArray[c] = (char)(c + 97);
  131. }
  132.  
  133. Console.Write("Tablica char = [ ");
  134. foreach (char s in charArray)
  135. {
  136. Console.Write(s + " ");
  137. }
  138. Console.Write("]\n");
  139. Console.ReadKey();
  140. }
  141. }
  142. }
Success #stdin #stdout 0.03s 33952KB
stdin
Standard input is empty
stdout
Tablica int  = [ 1 4 9 7 ]
Tablica char = [ a b c d e f ]