Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class GenericClass<T>
  7. {
  8. private T[] arr;
  9. private int count = 0;
  10. //constructor
  11. public GenericClass(int capacity)
  12. {
  13. this.arr = new T[capacity];
  14. }
  15.  
  16. //methods
  17. public void StrechArray()
  18. {
  19.  
  20. T[] tempArr = new T[arr.Length * 2];
  21. Array.Copy(arr, 0, tempArr, 0, arr.Length);
  22. arr = tempArr;
  23.  
  24. }
  25. public void AddElements(T elem)
  26. {
  27. if (count < arr.Length)
  28. {
  29. arr[count] = elem;
  30. count++;
  31. }
  32. else
  33. {
  34. StrechArray();
  35. arr[count] = elem;
  36. count++;
  37. }
  38. }
  39. public T AccesElement(int index)
  40. {
  41. if (index <= count)
  42. {
  43. return arr[index];
  44. }
  45. else
  46. {
  47. throw new IndexOutOfRangeException("The index is outsite the array!");
  48. }
  49. }
  50. public void DeleteElement(int index)
  51. {
  52. if (index <= count)
  53. {
  54. T[] tempArr = new T[arr.Length];
  55. int tempArrindex = 0;
  56. for (int fromBegToIndex = 0; fromBegToIndex < index; fromBegToIndex++, tempArrindex++)
  57. {
  58. tempArr[tempArrindex] = arr[fromBegToIndex];
  59. }
  60.  
  61. for (int toTheEnd = index + 1; toTheEnd < count; toTheEnd++, tempArrindex++) //moje i do count
  62. {
  63. tempArr[tempArrindex] = arr[toTheEnd];
  64. }
  65. Array.Copy(tempArr, 0, arr, 0, arr.Length);
  66. arr = tempArr;
  67. count--;
  68. }
  69. else
  70. {
  71. throw new IndexOutOfRangeException("The index is outsite the array!");
  72. }
  73. }
  74. public void InsertElement(int index, T element)
  75. {
  76. T[] tempArr = new T[arr.Length];
  77. int tempArrIndex = 0;
  78. if (count + 1 < arr.Length)
  79. {
  80. for (int i = 0; i < index; i++, tempArrIndex++)
  81. {
  82. tempArr[tempArrIndex] = arr[i];
  83. }
  84. tempArr[tempArrIndex] = element;
  85. tempArrIndex++;
  86. count++;
  87. for (int toTheEnd = index; toTheEnd < count; toTheEnd++, tempArrIndex++)
  88. {
  89. tempArr[tempArrIndex] = arr[toTheEnd];
  90. }
  91. Array.Copy(tempArr, 0, arr, 0, arr.Length);
  92. arr = tempArr;
  93. }
  94. else
  95. {
  96. StrechArray();
  97. InsertElement(index, element);
  98. }
  99. }
  100. public void ClearArr()
  101. {
  102. for (int i = 0; i < count; )
  103. {
  104. DeleteElement(i);
  105. }
  106. }
  107. public int FindIndexByElement(T element)
  108. {
  109. int index = Array.IndexOf(arr, element);
  110. return index;
  111. }
  112. public override string ToString()
  113. {
  114. StringBuilder allElements = new StringBuilder();
  115. for (int i = 0; i < count; i++)
  116. {
  117. allElements.AppendFormat("{0,10} is the {1} element of the array",arr[i].ToString(),(i+1));
  118. allElements.AppendLine();
  119. }
  120. string final = allElements.ToString();
  121. return final;
  122. }
  123. //adding Max and Min methods
  124. public T Max<T>() where T : System.IComparable<T>, IComparable
  125. {
  126. dynamic max = arr[0];
  127. for (int i = 0; i < count; i++)
  128. {
  129. T tempValue = (dynamic)this.arr[i];
  130. if (tempValue.CompareTo(max)>0)
  131. {
  132. max = arr[i];
  133. }
  134. }
  135. return max;
  136. }
  137. public T Min<T>() where T : System.IComparable<T>, IComparable
  138. {
  139. dynamic min = arr[0];
  140. for (int i = 0; i < count; i++)
  141. {
  142. T tempValue = (dynamic)this.arr[i];
  143. if (tempValue.CompareTo(min) < 0)
  144. {
  145. min = arr[i];
  146. }
  147. }
  148. return min;
  149. }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement