Implement ArrayList in js

Source: Internet
Author: User

Implement ArrayList in js
1. Summary of the ArrayList Method

Constructor abstract ArrayList () constructs an empty list with an initial capacity of 10. ArrayList (Collection
 C) construct a list of elements that contain the specified collection. These elements are arranged in the order returned by the collection iterator. ArrayList (int initialCapacity) constructs an empty list with the specified initial capacity. Method abstract booleanadd (E e) adds the specified element to the end of this list. Voidadd (int index, E element) inserts the specified element into the specified position in this list. BooleanaddAll (Collection
 C) add all the elements in the collection to the end of the list in the order returned by the iterator of the specified collection. BooleanaddAll (int index, Collection
 C) Insert all elements in the specified collection to this list starting from the specified position. Voidclear () removes all elements from this list. Objectclone () returns a superficial copy of The ArrayList instance. Booleancontains (Object o) returns true if the list contains the specified element. VoidensureCapacity (int minCapacity) increases the capacity of this ArrayList instance if necessary to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter. Eget (int index) returns the element at the specified position in this list. IntindexOf (Object o) returns the index of the specified element that appears for the first time in this list, or-1 if this list does not contain elements. BooleanisEmpty () if there is no element in this list, true intlastIndexOf (Object o) is returned to the index of the last specified element in this list, or if this list does not contain an index, -1 is returned. Eremove (int index) removes the elements at the specified position in this list. Booleanremove (Object o) removes the specified Element (if any) that appears for the first time in this list ). Protected voidremoveRange (int fromIndex, int toIndex) removes all elements in the list between fromIndex (included) and toIndex (excluded. Eset (int index, E element) replaces the element at the specified position in this list with the specified element. Intsize () returns the number of elements in this list. Object [] toArray () returns an array containing all elements in the list in the appropriate order (from the first to the last element.
 
  
T [] toArray (T [] a) returns an array containing all elements in the 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. VoidtrimToSize (): Adjust the capacity of the ArrayList instance to the current size of the list.
 


2. js implementation of some functions
<Script type = "text/javascript" src = "json. js"> </script><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
 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.