Click to See Complete Forum and Search --> : 2d array problem


pelegk1
12-04-2003, 04:58 AM
i want to make a dynamic 2d aray
i did this code
but on line that is marked as (888)
i get an error!
why?
thanks in advance
peleg

function OpenCloseDiv(obj,val){
flag=false;

for (i=0;i<gArrayOfObj.length;++i){
alert("loop");
if (gArrayOfObj[i][0]==obj){
flag=true;
if (gArrayOfObj[1]) {
OpenAll(val);
}else{
CloseAll(val);
}
}
}
if (!flag){
alert(1);
gArrayOfObj[gArrayOfObj.length]=new Array(2);(***)
gArrayOfObj[gArrayOfObj.length][0]=obj;
gArrayOfObj[gArrayOfObj.length][1]=val;
alert(2);

}
for (i=0;i<gArrayOfObj.length;++i){
alert(gArrayOfObj[gArrayOfObj.length][0]+" "

+gArrayOfObj[gArrayOfObj.length][1]);
}
}

pelegk1
12-04-2003, 05:00 AM
sorry its the next line after (***)

Gollum
12-04-2003, 07:13 AM
I'll explain step by step what's going wrong, and hopefully the answer will appear before your very eyes :)

your first statement pushes an array onto the end of the array gArrayOfObj. Note that after this line, gArrayOfObj.length will be increased by one.
gArrayOfObj[gArrayOfObj.length]=new Array(2);

Now you are trying to access the first element of an array from one position after the last element in gArrayOfObj.
gArrayOfObj[gArrayOfObj.length][0]=obj;
gArrayOfObj[gArrayOfObj.length][1]=val;

Since there is no element after the last one, hopefully you can see why those two lines won't work. :cool:

pelegk1
12-04-2003, 08:29 AM
missed it