Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Defining_Classes_Part_2
  6. {
  7. public class GenericList<T> : ICountable, IGenericList<T>, IGenericList
  8. {
  9.  
  10. private const int _defaultCapacity = 4;
  11. private T[] _elements;
  12. private int _count;
  13. private int _capacity;
  14.  
  15. public GenericList()
  16. {
  17. this._elements = new T[_defaultCapacity];
  18. this._capacity = _defaultCapacity;
  19. }
  20.  
  21. public GenericList(int capacity)
  22. {
  23. this._elements = new T[capacity];
  24. this._capacity = capacity;
  25. }
  26.  
  27. public int Count
  28. {
  29. get
  30. {
  31. return this._count;
  32. }
  33. }
  34.  
  35. public void Add(T element)
  36. {
  37. if (_count == _elements.Length)
  38. {
  39. Expand();
  40. }
  41. this._elements[this._count++] = element;
  42. }
  43.  
  44. public T this[int index]
  45. {
  46. get
  47. {
  48. if (index >= _count)
  49. {
  50. throw new IndexOutOfRangeException();
  51. }
  52. return this._elements[index];
  53. }
  54. set
  55. {
  56. if (index >= _count)
  57. {
  58. throw new IndexOutOfRangeException();
  59. }
  60. this._elements[index] = value;
  61. }
  62. }
  63.  
  64. public void RemoveAt(int index)
  65. {
  66. if (index >= _count)
  67. {
  68. throw new IndexOutOfRangeException();
  69. }
  70. for (int i = index; i < this._count -1; i++)
  71. {
  72. _elements[i] = _elements[i + 1];
  73. }
  74. _elements[this._count - 1] = default(T);
  75. _count--;
  76. }
  77.  
  78. public override string ToString()
  79. {
  80. var result = new StringBuilder();
  81.  
  82. for (var i = 0; i < this.Count; i++)
  83. {
  84. result.Append(this._elements[i]);
  85.  
  86. if (i != this.Count - 1)
  87. {
  88. result.Append(", ");
  89. }
  90. }
  91. return result.ToString();
  92. }
  93.  
  94. private void Expand()
  95. {
  96. var newElements = new T[2 * this._capacity];
  97. this._capacity *= 2;
  98.  
  99. for (int i = 0; i < _count; i++)
  100. {
  101. newElements[i] = _elements[i];
  102. }
  103.  
  104. this._elements = newElements;
  105. }
  106.  
  107. public void Clear()
  108. {
  109. for (int i = 0; i < _count; i++)
  110. {
  111. this._elements[i] = default(T);
  112. }
  113. }
  114.  
  115. public int Find(T element)
  116. {
  117. string searchFor = element.ToString();
  118. for (int i = 0; i < _count; i++)
  119. {
  120. if (this._elements[i].ToString() == searchFor)
  121. {
  122. return i;
  123. }
  124. }
  125. return -1;
  126. }
  127.  
  128. public void InsertAt(T element, int index)
  129. {
  130. if (_count == _elements.Length)
  131. {
  132. Expand();
  133. }
  134.  
  135. this.Add(this._elements[_elements.Length - 1]);
  136.  
  137. for (int i = _count - 1; i > index; i--)
  138. {
  139. _elements[i] = _elements[i - 1];
  140. }
  141.  
  142. _elements[index] = element;
  143. }
  144. }
  145.  
  146. interface ICountable
  147. {
  148. int Count { get; }
  149. }
  150.  
  151. interface IGenericList<T>
  152. {
  153. void Add(T element);
  154. void RemoveAt(int index);
  155. void Clear();
  156. void InsertAt(T element, int Index);
  157. }
  158.  
  159. interface IGenericList
  160. {
  161. void RemoveAt(int index);
  162. void Clear();
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement