flex vector

写了一个vector类用来对数组进行维护,到new vector()一直报错,原来flex 中有vector,后来将该类名字换了,就不报错了。


import mx.collections.ArrayCollection;

	public class VArray
	{
		//向量元素的存储列表
		private var _source:ArrayCollection=new ArrayCollection();
		//当前访问的向量元素的索引
		private var _curIndex:int=-1;

		public function VArray()
		{
			_source=new ArrayCollection();
			
			_curIndex=-1;
		}
		
		/**
		 * 向向量末尾添加一个元素
		 * param element
		 * 			要添加的元素
		 */
		public function add(element:Object):void
		{
			_source.addItem(element);
			trace("add a element at last index");
		}
		
		/**
		 * 向向量中某一个位置添加一个元素
		 * param element
		 * 			要添加的元素
		 * param index
		 * 			向量中的位置。如果小于0,则默认为第一个位置;如果大于向量长度,则默认为末尾位置
		 */
		public function addAt(element:Object, index:int):void
		{
			if (index < 0)
				index=0;
			
			if (index > size())
				index=size();
			
			_source.addItemAt(element, index);
			trace("add a element at index");
		}
		
		/**
		 * 获取向量中某一位置的元素
		 * param index
		 * 			元素位置,当该位置<0或>size()时,返回空元素
		 */
		public function getItemAt(index:int):Object
		{
			if (index < 0 || index > size())
			{
				return null
				trace("index is ")
			}
			
			return _source.getItemAt(index);
		}
		
		/**
		 * 清空向量
		 *
		 */
		public function clear():void
		{
			_source.removeAll();
		}
		
		/**
		 * 删除某一位置上的元素,同时返回该元。当该位置<0或>size()时,返回空元素
		 * param index
		 * 			元素位置
		 */
		public function removeItemAt(index:int):Object
		{
			if (index < 0)
				return null;
			if (index > size())
				return null;
			
			return _source.removeItemAt(index);
		}
		
		/**
		 * 返回向量中元素的个数
		 *
		 */
		public function size():Number
		{
			return _source.length;
		}
		
		/**
		 * 获取当前元素的下一个元素,如果没有,则返回空元素
		 * return 下一个元素
		 */
		public function next():Object
		{
			var element:Object=null;
			
			if (hasNext())
			{
				_curIndex++;
				element=_source.getItemAt(curIndex);
				trace("has next");
			}
			
			return element;
		}
		
		/**
		 * 获取当前元素的上一个元素,如果没有,则返回空元素
		 * return 上一个元素
		 */
		public function previous():Object
		{
			var element:Object=null;
			
			if (hasPrevious())
			{
				_curIndex--;
				element=_source.getItemAt(curIndex);
				trace("has previous");
			}
			
			return element;
		}
		
		/**
		 * 判断是否有下一个元素
		 * return true
		 * 			有
		 * 		  false
		 * 			没有
		 */
		public function hasNext():Boolean
		{
			return curIndex < size() - 1;
		}
		
		/**
		 * 判断是否有上一个元素
		 * return true
		 * 			有
		 * 		  false
		 * 			没有
		 */
		public function hasPrevious():Boolean
		{
			return curIndex > 0;
		}
		
		/**
		 * 判断向量是否为空
		 * return true
		 * 			为空
		 * 		  false
		 * 			不为空
		 */
		public function isEmpty():Boolean
		{
			return _source.length == 0;
		}
		
		/**
		 * 获取当前元素索引,只读属性
		 *
		 */
		public function get curIndex():int
		{
			return _curIndex;
		}
		
		/**
		 * 获取全部元素的列表,只读属性
		 *
		 */
		public function get source():ArrayCollection
		{
			return _source;
		}

	}

你可能感兴趣的:(vector)