Реализовать IEnumerate и IEnumerable для собственного списка - C#

Узнай цену своей работы

Формулировка задачи:

Помогите пожалуйста имплантировать IEnumerate и IEnumerable. Было задание так же без никакого yiled! Встрял, таращусь уже несколько часов. Помогите пожалуйста. Привожу весь код.
Листинг программы
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MyList
  8. {
  9. sealed class MyList<T> : ICustomList<T>, IEnumerable<T>, IEnumerator<T>
  10. {
  11. #region ConstructorsAndVariables
  12. T[] array;
  13. public int Count { get; private set; }
  14. const int DefaultCapacity = 4;
  15. public MyList()
  16. {
  17. array = new T[DefaultCapacity];
  18. }
  19. public MyList(int capacity)
  20. {
  21. ThrowIfCapacityIsLessThanZero(capacity);
  22. array = new T[capacity];
  23. }
  24. #endregion ConstructorsAndVariables
  25. #region Implementation
  26. // actualy we can use Array.Copy and Array.Resize for more efficient
  27. private void ArrayCopy(T[] source, int sourceIndexStart, T[] destination, int destinationIndexStart, int length)
  28. {
  29. while (length != 0)
  30. {
  31. destination[destinationIndexStart++] = source[sourceIndexStart++];
  32. length--;
  33. }
  34. }
  35. private void ResizeArray()
  36. {
  37. T[] tempArray = new T[array.Length * 2];
  38. ArrayCopy(array, 0, tempArray, 0, Count);
  39. array = tempArray;
  40. }
  41. public void Add(T item)
  42. {
  43. if (Count == array.Length)
  44. ResizeArray();
  45. array[Count++] = item;
  46. }
  47. public void Insert(int index, T item)
  48. {
  49. ThrowIfIndexIsOutOfRange(index);
  50. if (Count == array.Length)
  51. ResizeArray();
  52. // harder way
  53. //T[] tempArray = new T[array.Length];
  54. //ArrayCopy(array, 0, tempArray, 0, index);
  55. //ArrayCopy(array, index, tempArray, index + 1, size - index);
  56. //array = tempArray;
  57. for (int i = Count; i > index; i--)
  58. array[i] = array[i - 1];
  59. array[index] = item;
  60. Count++;
  61. }
  62. public bool Remove(T item)
  63. {
  64. int indexOfFindItem = LinearSearch(item);
  65. if (indexOfFindItem == -1)
  66. return false;
  67. RemoveAt(indexOfFindItem);
  68. ArrayCopy(array, indexOfFindItem + 1, array, indexOfFindItem, Count - indexOfFindItem);
  69. Count--;
  70. return true;
  71. }
  72. public void RemoveAt(int index)
  73. {
  74. ThrowIfIndexIsOutOfRange(index);
  75. ArrayCopy(array, index + 1, array, index, Count - index);
  76. Count--;
  77. }
  78. public int IndexOf(T item)
  79. {
  80. return LinearSearch(item);
  81. }
  82. public void Reverse()
  83. {
  84. for (int i = 0; i < Count/2; i++)
  85. {
  86. T temp = array[i];
  87. array[i] = array[Count - i - 1];
  88. array[Count - i - 1] = temp;
  89. }
  90. }
  91. public T this[int index]
  92. {
  93. get
  94. {
  95. ThrowIfIndexIsOutOfRange(index);
  96. return array[index];
  97. }
  98. set
  99. {
  100. ThrowIfIndexIsOutOfRange(index);
  101. array[index] = value;
  102. }
  103. }
  104. public int LinearSearch(T item)
  105. {
  106. for (int i = 0; i < Count; i++)
  107. if (array[i].Equals(item))
  108. return i;
  109. // can't compare in this was array[i] == item
  110. // can't overload T == T, compile forbid this action
  111. // Generates generate code on run time that's why we didn't know which kind of type is T
  112. // Then use to compare EqualityComparer<T>.Default.Equals(array[i], item)
  113. return -1;
  114. }
  115. #endregion Implementation
  116. #region Exceptions
  117. private void ThrowIfCapacityIsLessThanZero(int capacity)
  118. {
  119. if(capacity < 0)
  120. throw new ArgumentOutOfRangeException("Capacity can't be less then zero");
  121. }
  122. private void ThrowIfIndexIsOutOfRange(int index)
  123. {
  124. if (index > Count || index < 0)
  125. throw new ArgumentOutOfRangeException("Index out of range");
  126. }
  127. #endregion Exceptions
  128. #region IEnumerableAndIEnumarte
  129. private int position = -1;
  130. public T Current
  131. {
  132. get
  133. {
  134. return Current;
  135. }
  136. }
  137. object IEnumerator.Current
  138. {
  139. get
  140. {
  141. return array[position];
  142. }
  143. }
  144. public bool MoveNext()
  145. {
  146. if (position < Count)
  147. {
  148. position++;
  149. return true;
  150. }
  151. else
  152. {
  153. Reset();
  154. return false;
  155. }
  156. }
  157. public void Reset()
  158. {
  159. position = -1;
  160. }
  161. public IEnumerator<T> GetEnumerator()
  162. {
  163. }
  164. IEnumerator IEnumerable.GetEnumerator()
  165. {
  166. return GetEnumerator();
  167. }
  168. public void Dispose()
  169. {
  170. }
  171. #endregion IEnumerableAndIEnumarte
  172. }
  173. }

Решение задачи: «Реализовать IEnumerate и IEnumerable для собственного списка»

textual
Листинг программы
  1.             MyList<int> t = new MyList<int>(3);
  2.  
  3.             t.Add(1);
  4.             t.Add(2);
  5.             t.Add(3);
  6.             foreach (var i in t)
  7.             {
  8.                 foreach (var j in t)
  9.                 {
  10.                     Console.WriteLine("{0},{1}", i,j);
  11.                 }
  12.             }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.667 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы