Building ArrayList sample code in JavaScript _javascript tips

Source: Internet
Author: User
Tags addall javascript array

We introduced the JavaScript array API, which is very powerful in JavaScript, can store any type, automatically expands in length, and provides a way to iterate, filter, and so many arrays of operations.

Simply explode Java's array (length fixed, single type). And the collection class in Java is to make up for the lack of arrays, its bottom most use object [] storage, but provide dynamic expansion strategy, of course, the JDK API rich, is unmatched in other languages.

But it doesn't interfere with my love of Java and JavaScript.

Java is like a middle-aged old woman, you can always see her in the JDK admirable, in the construction of large-scale distributed system, it reflects her teachings;

And JavaScript is a budding maiden, each bloom, will arouse your inner ripples, must be carefully tuned, can be used for you.

Well, forgive me for the inappropriate metaphor, for some dry goods.

/** * @class ArrayList * @description * @time 2014-09-16 21:59 * @author Starzou **/function ArrayList (arr) {this._ Elementdata = Arr | | 
[]; var arraylistprototype = {' _arrayprototype ': Array.prototype, ' _getdata ': function () {return this._elementdata 
; 
}, ' Size ': function () {return this._getdata (). length; 
}, ' IsEmpty ': function () {return this.size () = 0; 
}, ' contains ': function (obj) {return this.indexof (obj) > 1; 
}, ' IndexOf ': function (obj) {var i, data = This._getdata (), length = Data.length; 
for (i = 0; i < length; i++) {if (obj = = Data[i]) {return i; 
}} return-1; 
}, ' LastIndexOf ': function (obj) {var i, data = This._getdata (), length = Data.length; 
for (i = length-1 i > 1; i--) {if (obj = = Data[i]) {return i; 
}} return-1; 
}, ' Get ': function (index) {return this._getdata () [index]; 
}, ' Set ': function (index, Element) {this._getdata () [index] = element; }, ' Add ': function (index, Element{if (element) {This.set (index, Element); 
else {return This._getdata (). push (index); 
}, ' Remove ': function (index) {var oldValue = This._getdata () [index]; 
This._getdata () [index] = null; 
return oldValue; 
}, ' Clear ': function () {this._getdata (). length = 0; 
}, ' AddAll ': function (index, array) {if (array) {This._getdata (). Splice (index, 0, array); 
else {this._arrayprototype.push.apply (This._getdata (), index); 

} 
} 

}; Arraylist.prototype = Arraylistprototype;
Test code 
var arr = new ArrayList ([3, 6, 5, ' xyz ', ' foo ', ' XYZ ']); 

Console.log (Arr.contains (' xyz ')); 
Console.log (Arr.indexof (' xyz ')); 
Console.log (Arr.lastindexof (' xyz ')); 
Console.log (Arr.get (2)); 
Arr.addall ([1, 2, 3]); 
Console.log (arr);

The code here is a part of the optimization,

Later there is time to write JavaScript to simulate the implementation of tree, Stack, Queue, MAP and other data structure classes.

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.