標籤:js arraylist list
1.ArrayList方法摘要
構造方法摘要ArrayList() 構造一個初始容量為 10 的空列表。ArrayList(Collection<? extends E> c) 構造一個包含指定 collection 的元素的列表,這些元素是按照該 collection 的迭代器返回它們的順序排列的。ArrayList(int initialCapacity) 構造一個具有指定初始容量的空列表。 方法摘要 booleanadd(E e) 將指定的元素添加到此列表的尾部。 voidadd(int index, E element) 將指定的元素插入此列表中的指定位置。 booleanaddAll(Collection<? extends E> c) 按照指定 collection 的迭代器所返回的元素順序,將該 collection 中的所有元素添加到此列表的尾部。 booleanaddAll(int index, Collection<? extends E> c) 從指定的位置開始,將指定 collection 中的所有元素插入到此列表中。 voidclear() 移除此列表中的所有元素。 Objectclone() 返回此 ArrayList 執行個體的淺表副本。 booleancontains(Object o) 如果此列表中包含指定的元素,則返回 true。 voidensureCapacity(int minCapacity) 如有必要,增加此 ArrayList 執行個體的容量,以確保它至少能夠容納最小容量參數所指定的元素數。 Eget(int index) 返回此列表中指定位置上的元素。 intindexOf(Object o) 返回此列表中首次出現的指定元素的索引,或如果此列表不包含元素,則返回 -1。 booleanisEmpty() 如果此列表中沒有元素,則返回 true intlastIndexOf(Object o) 返回此列表中最後一次出現的指定元素的索引,或如果此列表不包含索引,則返回 -1。 Eremove(int index) 移除此列表中指定位置上的元素。 booleanremove(Object o) 移除此列表中首次出現的指定元素(如果存在)。protected voidremoveRange(int fromIndex, int toIndex) 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之間的所有元素。 Eset(int index, E element) 用指定的元素替代此列表中指定位置上的元素。 intsize() 返回此列表中的元素數。 Object[]toArray() 按適當順序(從第一個到最後一個元素)返回包含此列表中所有元素的數組。<T> T[]toArray(T[] a) 按適當順序(從第一個到最後一個元素)返回包含此列表中所有元素的數組;返回數組的運行時類型是指定數組的運行時類型。 voidtrimToSize() 將此 ArrayList 執行個體的容量調整為列表的當前大小。
2.js實現部分功能
<html><script type="text/javascript" src="json.js"></script><head> <script type="text/javascript"> function ArrayList(){ this.arr=[], this.size=function(){ return this.arr.length; }, this.add=function(){ if(arguments.length==1){ this.arr.push(arguments[0]); }else if(arguments.length>=2){ var deleteItem=this.arr[arguments[0]]; this.arr.splice(arguments[0],1,arguments[1],deleteItem) } return this; }, this.get=function(index){ return this.arr[index]; }, this.removeIndex=function(index){ this.arr.splice(index,1); }, this.removeObj=function(obj){ this.removeIndex(this.indexOf(obj)); }, this.indexOf=function(obj){ for(var i=0;i<this.arr.length;i++){ if (this.arr[i]===obj) { return i; }; } return -1; }, this.isEmpty=function(){ return this.arr.length==0; }, this.clear=function(){ this.arr=[]; }, this.contains=function(obj){ return this.indexOf(obj)!=-1; } };//建立一個Listvar list=new ArrayList();//增加一個元素list.add("0").add("1").add("2").add("3");//增加指定位置list.add(2,"22222222222");//刪除指定元素list.removeObj("3");//刪除指定位置元素list.removeIndex(0);for(var i=0;i<list.size();i++){document.writeln(list.get(i));}document.writeln(list.contains("2")) </script></head><body></body></html>
js實現ArrayList功能