Home > Article > Web Front-end > js implements ArrayList function with example code
1. ArrayList method summary
Construction method summary
ArrayList()
Construct an empty list with an initial capacity of 10.
ArrayList(Collection2d4902c92e1e7bfd574f59708c57776a c)
Constructs a list containing the elements of the specified collection, arranged in the order in which they are returned by the collection's iterator.
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
Method summary
boolean add(E e)
Adds the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element into this list at the specified position.
boolean addAll(Collection2d4902c92e1e7bfd574f59708c57776a c)
Adds all elements in the collection to the end of this list in the order of elements returned by the iterator of the specified collection.
boolean addAll(int index, Collection2d4902c92e1e7bfd574f59708c57776a c)
Starting from the specified position, insert all elements in the specified collection into this list.
void clear()
Remove all elements in this list.
Object clone()
Returns a shallow copy of this ArrayList instance.
boolean contains(Object o)
Returns true if this list contains the specified element.
void ensureCapacity(int minCapacity)
If necessary, increase the capacity of this ArrayList instance to ensure that it can hold at least the number of elements specified by the minimum capacity parameter.
E get(int index)
Returns the element at the specified position in this list.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list contains no element.
boolean isEmpty()
Returns true if there is no element in this list
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or if this list does not Contains an index, returns -1.
E remove(int index)
Remove the element at the specified position in this list.
boolean remove(Object o)
Removes the first occurrence of the specified element in this list (if it exists).
protected void removeRange(int fromIndex, int toIndex)
Removes all elements in the list whose index is between fromIndex (inclusive) and toIndex (exclusive).
E set(int index, E element)
Replace the element at the specified position in this list with the specified element.
int size()
Returns the number of elements in this list.
Object[] toArray()
Returns an array containing all the elements in this list in appropriate order (from first to last element).
8742468051c85b06f0a0af9e3e506b5c T[] toArray(T[] a)
Returns an array containing all the elements in this list in appropriate order (from first to last element); the runtime type of the returned array is Specifies the runtime type of the array.
void trimToSize()
Adjust the capacity of this ArrayList instance to the current size of the list.
2.js implements some functions
<html> <script type="text/javascript" src="json.js?1.1.9"></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; } }; //新建一个List var 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>
Related articles:
Detailed introduction to using C# to describe data structure 3 :ArrayList graphic code