using System; public class ObjectArray : System.Object { private System.Object[] objectArray = null; private int nElementLength = 0; /// 构造 public ObjectArray(int objectLength) { objectArray = new System.Object[objectLength]; } /// 实际长度 public int Length { get { return objectArray.Length; } } /// 元素长度 public int ElementLength { get { return nElementLength; } } /// 添加 public void Add(System.Object obj) { if (obj == null) { obj = ""; //return; } if (nElementLength >= Length) { CDebug.LogError("数组索引越界" + obj.ToString()); return; } objectArray[nElementLength] = obj; nElementLength++; } /// 添加 public void AddRange(ObjectArray objectArray) { for (int i = 0; i < objectArray.ElementLength; i++) { this.Add(objectArray.Get(i)); } } /// 反响添加 public void AddReversalRange(ObjectArray objectArray) { for (int i = objectArray.ElementLength-1; i >= 0; i--) { this.Add(objectArray.Get(i)); } } public System.Object this[int index] { set { if (index >= objectArray.Length) return; objectArray[index] = value; if (index > ElementLength - 1) { nElementLength = index + 1; } } get { return objectArray[index]; } } /// 获得 public System.Object Get(int index) { return objectArray[index]; } /// 查找一个静态数据对象 public T Get(int index) { return (T)objectArray[index]; } /// 根据索引修改一个值 public System.Object Set(int index, System.Object obj) { if (index >= objectArray.Length) return null; objectArray[index] = obj; if (index > ElementLength - 1) { nElementLength = index + 1; } return obj; } /// 删除 public System.Object Del(System.Object obj) { System.Object returnObj = null; for (int i = 0; i < nElementLength; i++) { if (objectArray[i].Equals(obj)) { returnObj = objectArray[i]; objectArray[i] = null; FrontElement(i); break; } } return returnObj; } /// 插入 public void Insert(System.Object obj, int index) { AafterElement(index); objectArray[index] = obj; } /// 删除最后一个 public System.Object Pop() { System.Object result = objectArray[ElementLength - 1]; Del(result); return result; } /// 清空所有 public void SetEmpty() { for (int i = 0; i < nElementLength; i++) { objectArray[i] = null; } nElementLength = 0; } /// 指定对象的索引 public int IndexOf(System.Object obj) { for (int i = 0; i < nElementLength; i++) { if ((objectArray[i] != null) && objectArray[i].Equals(obj)) { return i; } } return -1; } /// 前移元素 public void FrontElement(int index) { if (index >= 0 && index < nElementLength) { for (int i = index; i < nElementLength - 1; i++) { objectArray[i] = objectArray[i + 1]; } objectArray[nElementLength - 1] = null; nElementLength--; } } /// 后移元素 public void AafterElement(int index) { objectArray[nElementLength] = objectArray[nElementLength - 1]; System.Object objCur = objectArray[index]; System.Object objNext = null; for (int i = index; i < nElementLength; i++) { objNext = objectArray[i + 1]; objectArray[i + 1] = objCur; objCur = objNext; } nElementLength++; } }