Home > Web Front-end > JS Tutorial > body text

js implements ArrayList function with example code

PHP中文网
Release: 2017-03-17 17:19:39
Original
1546 people have browsed it

1. ArrayList method summary

Construction method summary

ArrayList()
Construct an empty list with an initial capacity of 10.
ArrayList(Collection 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(Collection 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, Collection 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).
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>
Copy after login

Related articles:

Detailed introduction to using C# to describe data structure 3 :ArrayList graphic code

How to implement C# copycat ArrayList in PHP

ArrayList sample code analysis of Java collection

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!