Java List using JavaScript (example)

Source: Internet
Author: User

Copy codeThe Code is as follows:
/**
* Js simulates List in java
*/

Var list = new Array ();

/**
* Add
* @ Param {Object} object
*/
Function add (object ){
List [list. length] = object;
}

/**
* Remove the elements at the specified position in the list.
* @ Param index specifies the position
* @ Return elements at this position
*/
Function removeIndex (index ){
Var object = this. list [index];
This. list. splice (index, 1 );
Return object;
}

/**
* Remove the specified element from the list.
* @ Param object specifies the element
* @ Return elements at this position
*/
Function remove (object ){
Var I = 0;
For (; I <list. length; I ++ ){
If (list [I] === object ){
Break;
}
}
If (I> = list. length ){
Return null;
} Else {
Return removeIndex (I );
}
}

/**
* Obtain the specified Element in the list.
* @ Param object specifies the element
* @ Return elements at this position
*/
Function get (index ){
Return list [index];
}

/**
* Remove all elements from the list.
*/
Function removeAll (){
List. splice (0, list. length );
}

/**
* Returns the number of elements in the list.
* @ Return number of elements
*/
Function size (){
Return this. list. length;
}


/**
* If the list does not contain elements, true is returned.
* @ Return true or false
*/
Function isEmpty (){
Return list. length = 0;
}

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.