Page 1 of 1

My arraylist class

Posted: Tue Jul 31, 2007 12:03 am
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

Posted: Tue Sep 04, 2007 7:54 pm
by Gyanu
WHT it this?

Posted: Wed Sep 05, 2007 7:12 am
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.

Posted: Sun Sep 09, 2007 7:38 pm
by Flipper3
I am confused though...can't you use arrays without that code?

Posted: Fri Sep 14, 2007 2:06 pm
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

Posted: Sat Sep 15, 2007 9:30 pm
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? :/

Posted: Sun Sep 16, 2007 9:09 am
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.

Posted: Sun Sep 16, 2007 3:51 pm
by Flipper3
Ahh!!! I get it now. Thanks!!! :D