Home > Web Front-end > JS Tutorial > body text

Using JavaScript to implement Java's List function (explanation with examples)_javascript skills

WBOY
Release: 2016-05-16 17:16:59
Original
1284 people have browsed it

复制代码 代码如下:

/**
* js simulates List in java
*/

var list = new Array();

/**
* Add
* @param {Object} object
*/
function add(object) {
 list[list.length] = object;
}

/**
* Remove the element at the specified position from this list.
* @param index specifies the position
* @return the element at this position
*/
function removeIndex(index) {
 var object = this.list[index];
 this.list.splice(index, 1);
 return object;
}

/**
* Remove the specified element from this list.
* @param object specified element
* @return element 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);
 }
}

/**
* Get the specified element in the list.
* @param object specified element
* @return element at this position
*/
function get(index) { 
    return list[index]; 

/**
* Remove all elements from this list.
*/ 
function removeAll() { 
    list.splice(0, list.length); 
}

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

 
/**
* Returns true if the list contains no elements.
* @return true or false
*/ 
function isEmpty() { 
    return list.length == 0; 
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template