JS implementation ArrayList function attached instance code

Source: Internet
Author: User
Tags addall int size shallow copy

1.ArrayList Method Summary

Construction Method Summary

ArrayList ()
constructs an empty list with an initial capacity of 10.
ArrayList (collection<.? extends e> c)
Constructs a list of elements that contain the specified Collection that are listed in the order in which they are returned by the Collection iterator.
ArrayList (int initialcapacity)
Constructs an empty list with the specified initial capacity.
Method Summary
Boolean Add (E)
adds the specified element to the tail of this list.
void Add (int index, E Element)
Inserts the specified element into this list at the specified location.
Boolean AddAll (collection<. Extends e> c)
adds all the elements in the Collection to the end of this list according to the order of the elements returned by the iterator for the specified Collection.
Boolean addall (int index, collection<. Extends e> c)
Inserts all the elements in the specified Collection into this list, starting at the specified location.
void Clear ()
removes all elements from this list.
Object Clone ()
Returns a shallow copy of this ArrayList instance.
Boolean contains (Object O)
Returns True if the specified element is included in this list.
void ensurecapacity (int mincapacity)
increases the capacity of this ArrayList instance if necessary to ensure that it can at least accommodate 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 specified element that first appears in this list, or 1 if the list does not contain elements.
Boolean isempty ()
If there are no elements in this list, return true
int lastindexof (Object o)
returns to this listThe index of the specified element that was last seen, or 1 if the list does not contain an index.
E-Remove (int index)
Removes the element from the specified position in this list.
Boolean remove (Object o)
Removes the first occurrence of the specified element, if present, in this list.
protected void RemoveRange (int fromindex, int toindex)
Removes all elements in the list that are indexed between fromindex (including) and Toindex (excluding).
e Set (int index, E Element)
replaces 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 of all the elements in this list, in the appropriate order (from first to last element).
<T> t[] ToArray (t[] a)
returns an array of all the elements in this list, in the appropriate order (from first to last element); The run-time type of the array is the Run-time type of the specified array.
void TrimToSize ()
Adjusts the capacity of this ArrayList instance to the current size of the list.

2.js Implement some functions

The

code is as follows:


&lt;html&gt;


&lt;script type= "Text/javascript" src= "json.js" &gt;&lt;/script&gt;


&lt;head&gt;


&lt;script type= "Text/javascript" &gt;


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&gt;=2) {


var deleteitem=this.arr[arguments[0]];


This.arr.splice (Arguments[0],1,arguments[1],deleteitem)


}


return to 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&lt;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");


//Add specified position


List.add (2, "22222222222");


//delete specified element


list.removeobj ("3");


//Delete the specified location element


list.removeindex (0);





for (var i=0;i&lt;list.size (); i++) {


Document.writeln (List.get (i));


}


Document.writeln (List.contains ("2"))


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;/body&gt;





&lt;/html&gt;

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.