Skip to content

Instantly share code, notes, and snippets.

@egermano
Created September 11, 2012 15:02
Show Gist options
  • Save egermano/3699467 to your computer and use it in GitHub Desktop.
Save egermano/3699467 to your computer and use it in GitHub Desktop.
Array List
function ArrayList(initialLength) {
this.length = 0;
this.array = new Array(initialLength);
this.add = function(value) {
if (this.length == this.array.length){
this.grow();
}
this.array[this.length++] = value;
};
this.grow = function() {
var original = this.array;
this.array = new Array(this.length * 2);
for(var i = 0; i < this.length; ++i) {
this.array[i] = original[i];
};
}
}
var array = new ArrayList(1);
array.add(2);
array.add(9);
array.add(4);
console.log(array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment