ArrayList的实现

一直觉得AS3关于集合的API太少,可以说是没有,仅仅提供了Object、Array、Vector等,这些可以满足日常的编程需求,不过,我觉得有点不足,于是就稍稍封装了一下这块。

主要接口的结构图为

ArrayList实现了IList全部接口,其具体实现代码如下:

package tunie.struct.group
{
	import tunie.struct.EqualsUtil;
	import tunie.struct.error.ErrorDesc;

	/**
	 * Tunie
	 * Oct 8, 2015 3:46:22 PM
	 * <b>List主要功能如下</b>
	 * <li>列表
	 */
	public class ArrayList extends Group implements IList , IGroup
	{
		protected var _list:Array;

		public function ArrayList()
		{
			_list = [];
		}
		//pass
		public function get size():int
		{
			return _list.length;
		}
		//pass
		public function add(value:*):void
		{
			_list.push(value);
		}
		//pass
		public function addList(list:IList):void
		{
			if(list == null) return ;
			if(list is ArrayList) {
				addArr((list as ArrayList).content);
			} else {
				for(var i:int = 0; i < list.size ;i ++) {
					add(list.obtain(i));
				}
			}
		}
		//pass
		public function addArr(list:Array):void {
			if(list == null) return ; 
			for(var i:int = 0; i < list.length ; i++) {
				add(list[i]);
			}
		}
		//pass
		public function alter(value:*, toValue:*):void
		{
			var indexList:IList = obtainIndexList(value);
			if(indexList == null) return;
			for(var i:int = 0; i < indexList.size; i++) {
				alterAt(indexList.obtain(i) , toValue);
			}
		}
		//pass
		public function alterAt(index:int, value:*):*
		{
			index = checkIndex(index);
			var oriValue:* = obtain(index);
			_list[index] = value;
			return oriValue;
		}
		//pass
		public function alterList(startIndex:int, endIndex:int, list:IList):IList
		{
			var tempList:IList = removeRangeList(startIndex , endIndex);
			insertList(list , startIndex);
			return tempList;
		}
		//pass
		public function clearRepeat():IList
		{
			if(isEmpty) return null;
			var indexList:IList = new ArrayList();
			for(var i:int = 0 ; i < size ; i++) {
				if(indexList.contain(i)) continue;
				var tempList:IList = obtainIndexList(obtain(i));
				tempList.removeAt(0);
				indexList.addList(tempList);
			}
			return removeIndexList(indexList);
		}
		//pass
		public function insert(value:*, index:int=0):void
		{
			index = checkIndex(index);
			_list.splice(index , 0 , value);
		}
		//pass
		public function insertList(list:IList, index:int=0):void
		{
			if(list == null) return;
			if(isEmpty || index >= size) addList(list);
			for(var i:int = list.size - 1; i >= 0 ; i--) {
				insert(list.obtain(i) , index);
			}
		}
		//pass
		public function obtain(index:int=0):*
		{
			index = checkIndex(index);
			return _list[index];
		}
		//pass
		public function obtainIndex(value:*):int
		{
			if(isEmpty) return -1;
			for(var i:int = 0; i < size ; i++) {
				var tempValue:* = obtain(i);
				if(EqualsUtil.checkEquals(value , obtain(i))) return i;
			}
			return -1;
		}
		//pass
		public function obtainIndexList(value:*):IList
		{
			if(isEmpty) return null;
			var list:IList = new ArrayList();
			for(var i:int = 0; i < size ; i++) {
				var tempValue:* = obtain(i);
				if(EqualsUtil.checkEquals(value , obtain(i))) {
					list.add(i);
				}
			}
			return list;
		}
		//pass
		public function remove(value:*):Boolean
		{
			if(isEmpty) return false;
			var indexList:IList = obtainIndexList(value);
			removeIndexList(indexList);
			return false;
		}
		//pass
		public function removeAt(index:int=0):*
		{
			if(isEmpty) return null;
			index = checkIndex(index);
			var oriValue:* = obtain(index);
			_list.splice(index , 1);
			return oriValue;
		}
		//pass
		public function removeIndexList(indexList:IList):IList
		{
			if(indexList.isEmpty) return null;
			var list:IList = new ArrayList();
			for(var i:int = indexList.size - 1; i >= 0; i--) {
				list.add(removeAt(indexList.obtain(i)));
			}
			return list;
		}
		//pass
		public function removeList(list:IList):void
		{
			if(list == null) return ;
			var indexList:IList = new ArrayList();
			for(var i:int = 0; i < list.size ; i++) {
				indexList.addList(obtainIndexList(list.obtain(i)));
			}
			removeIndexList(indexList);
		}
		//pass
		public function removeRangeList(startIndex:int, endIndex:int):IList
		{
			if(checkIndexRange(startIndex , endIndex)){
				var arrList:Array = _list.splice(startIndex , endIndex - startIndex);
				var list:IList = new ArrayList();
				list.addArr(arrList);
				return list;
			}
			return null;
		}
		//pass
		public function subList(startIndex:int, endIndex:int):IList
		{
			if(checkIndexRange(startIndex , endIndex)){
				var arrList:Array = _list.slice(startIndex , endIndex);
				var list:IList = new ArrayList();
				list.addArr(arrList);
				return list;
			}
			return null;
		}

		override public function clear():void
		{
			_list = [];
		}
		//pass
		override public function contain(value:*):Boolean
		{
			if(isEmpty) return false;
			var index:int = obtainIndex(value);
			return index >= 0;
		}
		//pass
		override public function get isEmpty():Boolean
		{
			return size == 0;
		}

		/**
		 * 取得数组(ArrayList特有)
		 * @return 
		 */
		//pass
		public function get content():Array {
			return _list;
		}
		/**
		 * 检测索引值
		 * @param value
		 * @return 
		 */
		//pass
		protected function checkIndex(value:int):int {
			if(isEmpty) value = 0;
			if(value >= size - 1) return size - 1;
			return value;
		}
		/**
		 * 检测startIndex到endIndex范围,并返回取值是否合理
		 * @param startIndex
		 * @param endIndex
		 * @return 
		 */
		//pass
		protected function checkIndexRange(startIndex:int , endIndex:int):Boolean {
			if(isEmpty) return false;
			if(startIndex >= endIndex) {
				ErrorDesc.throws("传入的参数列表有误,startIndex只能小于endIndex");
			}
			if(startIndex >= size) {
				ErrorDesc.throws("传入的参数列表有误,startIndex不可超出列表最大索引");
			}
			if(endIndex > size) {
				ErrorDesc.throws("传入的参数列表有误,endIndex不可超出列表最大索引");
			}
			return true;
		}

	}
}

代码注释很清楚,也很简洁,若有不是很明白的地方,可以回复提出。下面贴出依赖的类

 IGroup

package tunie.struct.group
{
	/**
	 * Tunie
	 * Oct 8, 2015 3:51:26 PM
	 * <b>IGroup主要功能如下</b>
	 * <li>组群
	 */
	public interface IGroup
	{
		/**
		 * 清空
		 */
		function clear():void;
		/**
		 * 返回是否为空,空为true,不为空为false
		 * @return 
		 */
		function get isEmpty():Boolean;
		/**
		 * 返回是否包含value值,包含为true,不包含为false
		 * @param value
		 * @return 
		 */
		function contain(value:*):Boolean;
	}
}

Group

package tunie.struct.group
{
	import tunie.struct.error.ErrorDesc;

	/**
	 * Tunie
	 * Oct 8, 2015 4:00:29 PM
	 * <b>Group主要功能如下</b>
	 * <li>1.
	 */
	public class Group implements IGroup
	{
		public function Group()
		{
			clear();
		}

		public function clear():void
		{
			ErrorDesc.throwsNoRewrite(this , "clear");
		}

		public function get isEmpty():Boolean
		{
			ErrorDesc.throwsNoRewrite(this , "isEmpty");
			return false;
		}

		public function contain(value:*):Boolean
		{
			ErrorDesc.throwsNoRewrite(this , "contain");
			return false;
		}
	}
}

 ErrorDesc

package tunie.struct.error
{
	/** 
	 * May 10, 2015
	 * author: Tunie
	 * function desc:
	 * 抛出异常描述
	 */
	public class ErrorDesc
	{
		/** 抛出没有重写父类方法 */
		public static function throwsNoRewrite(target:* , fnName:String = ""):void {
			throws(target + "没有重写方法" + fnName);
		}
		/** 抛出无法实例化异常 */
		public static function throwsCanotInstance(target:*):void {
			throws(target + "无法直接实例化");
		}
		/** 抛出指定描述的异常 */
		public static function throws(desc:String , params:Object = null , id:* = 0):void {
			throw new MyError(desc , params , id);
		}

	}
}

    MyError

package tunie.struct.error
{
	/** 
	 * May 10, 2015
	 * author: Tunie
	 * function desc:
	 * 
	 */
	public class MyError extends Error
	{
		private var _params:Object;
		public function MyError(message:*="",params:Object = null , id:*=0)
		{
			this.params = params;
			super(message, id);
		}

		public function get params():Object
		{
			return _params;
		}

		public function set params(value:Object):void
		{
			_params = value;
		}

	}
}

     IList

package tunie.struct.group
{
	/**
	 * Tunie
	 * Oct 8, 2015 4:35:03 PM
	 * <b>IList主要功能如下</b>
	 * <li>列表
	 */
	public interface IList extends IGroup
	{
		/**
		 * 列表长度 
		 * @return 
		 */
		function get size():int;
		/**
		 * 在尾部新增一个元素
		 * @param value
		 */
		function add(value:*):void;
		/**
		 * 在尾部新增一个列表
		 * @param list
		 */
		function addList(list:IList):void;
		/**
		 * 添加一个数组到列表,数组中的每个元素做为列表中的一项
		 * @param list
		 */
		function addArr(list:Array):void;
		/**
		 * 往指定index插入索引
		 * @param value 值
		 * @param index 索引
		 */
		function insert(value:* , index:int = 0):void;
		/**
		 * 在指定index插入列表,
		 * @param list
		 * @param index
		 */
		function insertList(list:IList , index:int = 0):void;
		/**
		 * 移除列表中指定值(全部)
		 * @param value
		 */
		function remove(value:*):Boolean;
		/**
		 * 移除指定index的值
		 * @param index
		 * @return 被移除的值
		 */
		function removeAt(index:int = 0):*;
		/**
		 * 移除列表中的多个指定值
		 * @param startIndex
		 * @param endIndex
		 * @return 被移除的子列表
		 */
		function removeList(list:IList):void ;
		/**
		 * 移除列表中的指定索引值
		 * @param list
		 * @return 
		 */
		function removeIndexList(indexList:IList):IList;
		/**
		 * 移除从startIndex到endIndex的值,不包含endIndex
		 * @param startIndex
		 * @param endIndex
		 * @return 被移除的子列表
		 */
		function removeRangeList(startIndex:int , endIndex:int):IList ;
		/**
		 * 修改指定值
		 * @param value
		 */
		function alter(value:* , toValue:*):void;
		/**
		 * 修改指定index的值
		 * @param index
		 * @param value
		 * @return 
		 */
		function alterAt(index:int , value:*):*;
		/**
		 * 修改从startIndex到endIndex的值为指定的list,不包含endIndex
		 * @param startIndex
		 * @param endIndex
		 * @param list
		 * @return 被修改的子列表
		 */
		function alterList(startIndex:int , endIndex:int , list:IList):IList;
		/**
		 * 获取索引index的值
		 * @param index
		 * @return 
		 */
		function obtain(index:int = 0):*;
		/**
		 * 获取值的首个索引
		 * @param value
		 * @return 
		 */
		function obtainIndex(value:*):int;
		/**
		 * 获取值在列表中的所有索引
		 * @param value
		 * @return 
		 */
		function obtainIndexList(value:*):IList;
		/**
		 * 取得从startIndex到endIndex的子列表,不包含endIndex
		 * @param startIndex 开始位置
		 * @param endIndex 结束位置
		 * @return 
		 */
		function subList(startIndex:int , endIndex:int):IList;
		/**
		 * 清除重复的项
		 * @return 被清除的项列表
		 */
		function clearRepeat():IList;
	}
}
时间: 2024-07-30 20:37:53

ArrayList的实现的相关文章

ArrayList以及Map小练

ArrayList常用方法 public static void main(String[] args) { List list = new ArrayList(); List list1 = new ArrayList(); for (int i = 0; i < 5; i++) { list.add(i, "string"+i);//add(E e)向某集合的尾部追加 list1.add(i, "string"+(i+10)); } List list2

java持有对象【2】ArrayList容器续解

此为JDK API1.6.0对ArrayList的解释. ArrayList 使用java泛型创建类很复杂,但是应用预定义的泛型很简单.例如,要想定义用来保存Apple对象的ArrayList,可以声明ArrayList<Apple>,尖括号内为类型参数,(可以为多个).它指定了容器可以保存的类型. 通过使用泛型,可以在编译期防止将错误类型的对象放置到容器中. ArrayList向上转型为List. 应该注意到,在将元素从List中取出时,类型转换不是必须的了.因为List在调用get()时会

手动添加arraylist注解类(Contact联系人对象)

因为在Java核心库不支持arraylist xml直接注解,这里可以自己写个小工具类 Contact.java: package com.newer.xml; import java.util.ArrayList; import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; import org.simp

Java中ArrayList和LinkedList区别

一般大家都知道ArrayList和LinkedList的大致区别:      1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针.      3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据. ArrayList和LinkedList是两个集合类,用于存储一系列的对象引用

ArrayList、string、string[]之间的转换

1.ArrarList 转换为 string[] : ArrayList list = new ArrayList(); list.Add("aaa"); list.Add("bbb"); string[] arrString = (string[])list.ToArray(typeof( string)) ; 2.string[] 转换为 ArrarList : ArrayList list = new ArrayList(new string[] { &quo

ArrayList&amp;LinkedList&amp;Map&amp;Arrays

Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将obj加入到调用类集合中,加入返回true 否则 返回 false Boolean addAll(Collection c) 将c中所有元素都加入到类集合中,都加入返回true否则 false Void clean() 从调用类集合中删除所有元素 Boolean contains(Object obj

ArrayList代码示例

package com.shushine.framework.第七章Java标准类库; import java.util.ArrayList; /** * * <p> * 描述该类情况 {@link 代表跟谁有关系} * </p> * * @author 王超 * @since 1.0 * @date 2016年10月24日 下午7:46:28 * @see 新建|修改|放弃 * @see com.shushine.framework.第七章Java标准类库.ArrayListDe

蓝鸥Unity开发基础二——课时22 ArrayList

一.ArrayList 集合:集合是种容器,在程序中,使用集合管理相关对象组 集合分为非泛型集合和泛型集合 推荐视频讲师博客:http://11165165.blog.51cto.com/ 二.非泛型集合:使用非泛型集合需要引起命名空间System.Collections ArrayList--可以根据需要动态增加的数组 Hashtable--用来存储键值对的哈希表 Queue--遵循先进先出的对列 Stack--遵循后进先出的栈 三.泛型集合:使用泛型集合需要引入命名空间System.Coll

1.21 ArrayList 用法练习。

import java.util.ArrayList; public class TestArrayList { public static void main(String[] args) { //练习 ArrayList add(对象) set(索引) size() 以及构造方法 ArrayList<> String a = "好好学习"; //定义插入的字符串 String b = "天天向上"; ArrayList<String> a

java的List接口的实现类 ArrayList,LinkedList,Vector 的区别

Java的List接口有3个实现类,分别是ArrayList.LinkedList.Vector,他们用于存放多个元素,维护元素的次序,而且允许元素重复. 3个具体实现类的区别如下: 1. ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中.当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制.移动.代价比较高.因此,它