My arraylist class

Any problem with javascript can be discussed here.
Post Reply
thetarget
Posts: 496
Joined: Sat Jul 09, 2005 9:10 am

My arraylist class

Post by thetarget »

Code: Select all

function ArrayList() {
this.a_list = new Array(); function add(value) {
var currentIdx = this.size();
this.a_list[currentIdx] = value;
}
function get(idx) {
return this.a_list[idx];
}function remove(idx) {
for (x = idx; x < this.a_list.length; x++) {
this.a_list[x] = this.a_list[x+1];
}
this.a_list[this.a_list.length-1] = null;
}function size() {
var t_size = this.a_list.length;
if (t_size > 0 && this.a_list[this.a_list.length-1] == null) {
t_size--;
}
return t_size;
}function move(idxFrom,idxTo) {
var t_value = this.a_list[idxFrom];
//direction: move down
if (idxFrom < idxTo) {
for (x = idxFrom; x < idxTo; x++) {
this.a_list[x] = this.a_list[x+1];
}
this.a_list[idxTo] = t_value;
} else if ( idxTo < idxFrom) { //direction: move up

for (x = idxFrom; x > idxTo; x--) {
this.a_list[x] = this.a_list[x-1];
}
this.a_list[idxTo] = t_value;
}
}

function exchange(idxChange,idxWith) {
var t_value = this.a_list[idxWith];
this.a_list[idxWith] = this.a_list[idxChange];
this.a_list[idxChange] = t_value;
}

this.move = move;
this.remove = remove;
this.get = get;
this.add = add;
this.size = size;
this.exchange = exchange;
}
Available for download here: arraylist.js
Just to make arrays more neat and simple. I created this Arraylist class myself.. Using with not much problem(there are still problems tho)

Any problems faced when using it, please post it here so that i can improve on it :) thanks


Gyanu
Posts: 338
Joined: Mon Jul 30, 2007 2:03 pm
Contact:

Post by Gyanu »

WHT it this?
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post by Lixas »

This is a javascript function, to work with javascript arrays. these functions can:
  • Get a value of specific array item.
  • Swap to items
  • move items
GYANU, i have a golden rule: if you don't know what is that, probably you don't need that :P

Note for developer: you should more comment your code, because sometimes it is difficult to understand what is going on ;) by the way, it's nice. develop it further and maybe you will do best JS class to work with arrays.
Image
Flipper3
Posts: 353
Joined: Tue Feb 28, 2006 12:34 am

Post by Flipper3 »

I am confused though...can't you use arrays without that code?
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post by Lixas »

sure you can, but if you want to manipulate with the, so you have to code your own functions. These are already-made functions for manipulating arrays in JS
Image
Flipper3
Posts: 353
Joined: Tue Feb 28, 2006 12:34 am

Post by Flipper3 »

Lixas wrote:sure you can, but if you want to manipulate with the, so you have to code your own functions. These are already-made functions for manipulating arrays in JS

Ok...I am really lost because I never really use JS. :/

Why would you manipulate arrays and what does that even mean? :/
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post by Lixas »

if you are creating a little bit sophisticated software, you probably will have to use arrays (I'm talking about every kind of programming languages: php, c++, delphi, JS or whatever else). So, these are pre-made functions for you to work with arrays in javascript language.
i will try to illustrate with an example:

we have an variable with array of digits:

Code: Select all

var a = array(1,3,5,7,54,46);
for example you don't really know what is third member of array, so you can use function get(item_on_array)

Code: Select all

var third_member = get(2)
and you will have variable third_member is equal to 5
the same are with other functions.
Image
Flipper3
Posts: 353
Joined: Tue Feb 28, 2006 12:34 am

Post by Flipper3 »

Ahh!!! I get it now. Thanks!!! :D
Post Reply