JavaScript: ArrayList


Example of use


var list = new ArrayList();
list.add("c");
list.add("f");
list.add("a");
list.add("d");
list.add("e");
list.add("b");
list.remove("d");
alert('get()' + list.get());
alert('getSortedAsc()' + list.getSortedAsc());
alert('getSortedDesc()' + list.getSortedDesc());

The JavaScript implementation of (pseudo) 
ArrayList for MSIE and Mozilla:


function ArrayList() {
    var _array = [];

    this.add = add;
    this.get = get;
    this.remove = remove;
    this.size = size;
    this.toString = toString;
    this.getElementIndex = getElementIndex;
    this.getSortedAsc = getSortedAsc;
    this.getSortedDesc = getSortedDesc;
    this.isElementExists = isElementExists;
    this.hasAnyNotNullElement = hasAnyNotNullElement;

    function add(value){
    _array.push(value);
    }

    function remove(value){
    var toDelete = getElementIndex(value);
    delete _array[toDelete];
    }

    function size(){
    return _array.length;
    }

    function toString(){
    var t = '';
    for(var i = 0; i < size(); i++){
      t += _array[i];
      if(i < size() 1t += ', ';
    }
    return t;
    }

    function getElementIndex(value){
    for(var i = 0; i < size(); i++)
      if(_array[i== valuereturn i;
    return 'false';
    }

    function isElementExists(value){
    return (getElementIndex(value== 'false') false true;
    }

    function hasAnyNotNullElement(){
    for(var i = 0; i < size(); i++){
      if(_array[i!= '' && typeof(_array[i]) != 'undefined')
        return true;
    }
    return false;
    }

    function get(){
    var tmp = [];
    var j = 0;
    for(var i = 0; i < size(); i++){
      if(typeof(_array[i]) != 'undefined'){
        tmp[j= _array[i];
        j++;
      }
    }
    return tmp;
    }

    function getSortedAsc(){
    var tmp = [];
    tmp = get();
    return tmp.sort();
    }

    function getSortedDesc(){
    var tmp = [];
    tmp = get();
    tmp.sort();
    return tmp.reverse();
    }
}






As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles