ObjectArray.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. public class ObjectArray : System.Object
  3. {
  4. private System.Object[] objectArray = null;
  5. private int nElementLength = 0;
  6. /// <summary>构造</summary>
  7. public ObjectArray(int objectLength)
  8. {
  9. objectArray = new System.Object[objectLength];
  10. }
  11. /// <summary>实际长度</summary>
  12. public int Length
  13. {
  14. get { return objectArray.Length; }
  15. }
  16. /// <summary>元素长度</summary>
  17. public int ElementLength
  18. {
  19. get { return nElementLength; }
  20. }
  21. /// <summary>添加</summary>
  22. public void Add(System.Object obj)
  23. {
  24. if (obj == null)
  25. {
  26. obj = "";
  27. //return;
  28. }
  29. if (nElementLength >= Length)
  30. {
  31. CDebug.LogError("数组索引越界" + obj.ToString());
  32. return;
  33. }
  34. objectArray[nElementLength] = obj;
  35. nElementLength++;
  36. }
  37. /// <summary>添加</summary>
  38. public void AddRange(ObjectArray objectArray)
  39. {
  40. for (int i = 0; i < objectArray.ElementLength; i++)
  41. {
  42. this.Add(objectArray.Get(i));
  43. }
  44. }
  45. /// <summary>反响添加</summary>
  46. public void AddReversalRange(ObjectArray objectArray)
  47. {
  48. for (int i = objectArray.ElementLength-1; i >= 0; i--)
  49. {
  50. this.Add(objectArray.Get(i));
  51. }
  52. }
  53. public System.Object this[int index]
  54. {
  55. set
  56. {
  57. if (index >= objectArray.Length)
  58. return;
  59. objectArray[index] = value;
  60. if (index > ElementLength - 1)
  61. {
  62. nElementLength = index + 1;
  63. }
  64. }
  65. get
  66. {
  67. return objectArray[index];
  68. }
  69. }
  70. /// <summary>获得</summary>
  71. public System.Object Get(int index)
  72. {
  73. return objectArray[index];
  74. }
  75. /// <summary>查找一个静态数据对象</summary>
  76. public T Get<T>(int index)
  77. {
  78. return (T)objectArray[index];
  79. }
  80. /// <summary>根据索引修改一个值</summary>
  81. public System.Object Set(int index, System.Object obj)
  82. {
  83. if (index >= objectArray.Length)
  84. return null;
  85. objectArray[index] = obj;
  86. if (index > ElementLength - 1)
  87. {
  88. nElementLength = index + 1;
  89. }
  90. return obj;
  91. }
  92. /// <summary>删除</summary>
  93. public System.Object Del(System.Object obj)
  94. {
  95. System.Object returnObj = null;
  96. for (int i = 0; i < nElementLength; i++)
  97. {
  98. if (objectArray[i].Equals(obj))
  99. {
  100. returnObj = objectArray[i];
  101. objectArray[i] = null;
  102. FrontElement(i);
  103. break;
  104. }
  105. }
  106. return returnObj;
  107. }
  108. /// <summary>插入</summary>
  109. public void Insert(System.Object obj, int index)
  110. {
  111. AafterElement(index);
  112. objectArray[index] = obj;
  113. }
  114. /// <summary>删除最后一个</summary>
  115. public System.Object Pop()
  116. {
  117. System.Object result = objectArray[ElementLength - 1];
  118. Del(result);
  119. return result;
  120. }
  121. /// <summary>清空所有</summary>
  122. public void SetEmpty()
  123. {
  124. for (int i = 0; i < nElementLength; i++)
  125. {
  126. objectArray[i] = null;
  127. }
  128. nElementLength = 0;
  129. }
  130. /// <summary>指定对象的索引</summary>
  131. public int IndexOf(System.Object obj)
  132. {
  133. for (int i = 0; i < nElementLength; i++)
  134. {
  135. if ((objectArray[i] != null) && objectArray[i].Equals(obj))
  136. {
  137. return i;
  138. }
  139. }
  140. return -1;
  141. }
  142. /// <summary>前移元素</summary>
  143. public void FrontElement(int index)
  144. {
  145. if (index >= 0 && index < nElementLength)
  146. {
  147. for (int i = index; i < nElementLength - 1; i++)
  148. {
  149. objectArray[i] = objectArray[i + 1];
  150. }
  151. objectArray[nElementLength - 1] = null;
  152. nElementLength--;
  153. }
  154. }
  155. /// <summary>后移元素</summary>
  156. public void AafterElement(int index)
  157. {
  158. objectArray[nElementLength] = objectArray[nElementLength - 1];
  159. System.Object objCur = objectArray[index];
  160. System.Object objNext = null;
  161. for (int i = index; i < nElementLength; i++)
  162. {
  163. objNext = objectArray[i + 1];
  164. objectArray[i + 1] = objCur;
  165. objCur = objNext;
  166. }
  167. nElementLength++;
  168. }
  169. }