Js implementation of ArrayList function with instance code

  • 2020-03-30 04:13:24
  • OfStack

1. Summary of ArrayList methods

Summary of construction method

ArrayList ()
Construct an empty list with an initial capacity of 10.
ArrayList (Collection< The & # 63; Extends E> C)
Constructs a list of elements that contain the specified collection in the order in which the iterator of the collection returns them.
ArrayList (int initialCapacity)
Constructs an empty list with the specified initial capacity.
Methods in this paper,
Boolean add (E, E)
Adds the specified element to the end of the list.
Void add(int index, E element)
Inserts the specified element into the specified position in this list.
Boolean addAll (Collection< The & # 63; Extends E> C)
Add all the elements in the collection to the end of the list in the order returned by the iterator specifying the collection.
Boolean addAll (int index, Collection< The & # 63; Extends E> C)
Inserts all the elements in the specified collection into this list, starting at the specified location.
Void the clear ()
Remove all elements from this list.
The Object clone ()
Returns a shallow copy of this ArrayList instance.
Boolean contains (Object o)
Returns true if the list contains the specified element.
Void ensureCapacity (int minCapacity)
If necessary, increase the capacity of this ArrayList instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter.
E get (int index)
Returns the element at the specified location in this list.
Int indexOf (Object o)
Returns the index of the specified element that first appears in this list, or -1 if the list contains no elements.
Boolean isEmpty ()
Returns true if there is no element in this list
Int lastIndexOf (Object o)
Returns the index of the specified element that last appeared in this list, or -1 if the list does not contain an index.
E the remove (int index)
Removes the element from the specified position in the list.
Boolean remove (Object o)
Removes the first occurrence of the specified element (if any) in this list.
Protected void removeRange(int fromIndex, int toIndex)
Remove all elements in the list that are indexed between fromIndex (included) and toIndex (not included).
E set(int index, E element)
Replaces the element at the specified location 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 the appropriate order (from the first to the last element).
< T> T [] toArray (T [] a)
Returns an array containing all the elements in this list in the appropriate order (from the first to the last element); The runtime type of the returned array is the runtime type of the specified array.
Void the 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"></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;
}

};

//Create a new List
var list=new ArrayList();
//Add an element
list.add("0").add("1").add("2").add("3");
//Adds the specified location
list.add(2,"22222222222");
//Deletes the specified element
list.removeObj("3");
//Deletes the specified location element
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: