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