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.