Construct the ArrayList sample code in JavaScript, And the arraylist sample code

Source: Internet
Author: User
Tags javascript array

Construct the ArrayList sample code in JavaScript, And the arraylist sample code

Previously we introduced the JavaScript Array API. In JavaScript, the Array itself is very powerful, can store any type, and the length is automatically resized, and provides traversal and filtering, methods of multiple operation arrays.

Java array (fixed length, single type ). The collection classes in Java make up for the shortcomings of arrays. Most of them use Object [] storage at the underlying layer, but they only provide dynamic resizing policies. Of course, JDK's rich APIs are unmatched by other languages.

But it does not affect my love for Java and JavaScript.

Java is like a middle-aged old woman. You can always see her charm in JDK. when building a large distributed system, it can reflect her strong education;

JavaScript is a young girl. Every time it blooms, it will arouse your inner ripple. You must be careful with your instruction before using it for your use.

Okay, forgive me for not doing the right thing.

/** *@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 above implements a part of it, and there are some optimizations,

In the future, we will have time to write JavaScript to simulate classes that implement data structures such as Tree, Stack, Queue, and Map.


How does servlet transmit an arraylist object to javascript to use this object?

Use <%> to write java code in the javascript tag of the page
For example
<Script>
<% List list = new ArrayList ();
For (int I = 0; I <list. size (); I ++ ){

%>

Var index = <% = list. get (I). toString () %>;
<%}

%>
</Script>

Is there a type similar to ArrayList in java in javascript? You can perform operations such as add remove.

You can use arrays directly. javascript Arrays can be used as arraylist stacks and deque. They are an all-powerful collection.

You can use push pop to add and delete items at the end of the team, shift unshift to add and delete images at the head of the team, splie to delete and add items in the array, and slice to cut the array.

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.