Home  >  Article  >  Web Front-end  >  Building ArrayList sample code in JavaScript_javascript tips

Building ArrayList sample code in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:36:061599browse

We introduced the API of JavaScript Array earlier. In JavaScript, array itself is very powerful. It can store any type, and its length is automatically expanded. It also provides traversal, filtering, and other methods for operating arrays.

It simply beats Java’s array (fixed length, single type). The collection class in Java is to make up for the lack of arrays. Most of its bottom layers use Object [] storage, which only provides a dynamic expansion strategy. Of course, the richness of JDK's API is difficult for other languages ​​to match.

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

Java is like a middle-aged woman. You can always see her charm in the JDK. When building a large distributed system, her sincere teachings can be reflected;

And JavaScript is like a budding girl. Every time it blooms, it will stir up ripples in your heart. It must be carefully trained before it can be used by you.

Okay, forgive me for the inappropriate metaphor, let’s talk about it.

/** 
*@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 代码 
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 above code implements part of it, and there are still optimization areas,

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

Statement:
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