Проблема в том, что вторая функция не пишет [object], а пишет undefıed
|
Извините, за потраченное время, попробовал по другому, объект нашелся, большое спасибо, вам плюсик, тема закрыта
|
Цитата:
|
Ren,
что именно у вас не так (если просто проблема с массивами массивов то вот пример): function fun(){ document.a = prompt('Введите длину массива mass.',''); document.a2 = prompt('Введите длину массивов массива mass.',''); if (/\d+(\.\d+)?/.test( document.a) != true){fun();return} if (/\d+(\.\d+)?/.test( document.a2) != true){fun();return} document.a = new Number( document.a); document.a2 = new Number( document.a2); document.mass = new Array(); for (i = 0; i < document.a; i++){ document.mass[i] = new Array(); for (k = 0; k < document.a2; k++){ document.mass[i][k] = Math.round((k + 40) * Math.random()); } } func(); } function func(){ document.a3 = prompt('Введите индекс элемента массива mass.',''); document.a4 = prompt('Введите индекс элемента массива массива mass.',''); document.a3 = new Number(document.a3); document.a4 = new Number(document.a4); if ((/\d+(\.\d+)?/.test(document.a3) != true) || (document.a3 >= document.a)){func();return} if ((/\d+(\.\d+)?/.test(document.a4) != true) || (document.a4 >= document.a2)){func();return} alert(document.mass[document.a3][document.a4]) } fun(); |
Цитата:
В котором первый элемент содержит длину подмассива, а второй -уже указатель, Элементы с нулевой длиной периодически удаляются чистильщиком по таймеру |
Двух мерный массив
Лучшее решение создать собственный оператор массивов, точнее контейнер
вот пример function vector() { this.begin=null; this.end=null; this.zn=null; this.size=function(){var s=0;this.zn=this.begin;while(this.zn!=null){s++;this.zn=this.zn.end;}return s;} this.add_back=function(buff){this.zn=new vector();this.zn.begin=this.end;this.zn.end=null;this.zn.zn=buff;if(this.end==null)this.begin=this.zn;else this.end.end=this.zn;this.end=this.zn;} this.add_front=function(buff){this.zn=new vector();this.zn.begin=null;this.zn.end=this.begin;this.zn.zn=buff;if(this.begin==null)this.end=this.zn;else this.begin.begin=this.zn;this.begin=this.zn;} this.add=function(pos,buff){if(pos<=0)this.add_front(buff);else if(pos>=this.size())this.add_back(buff);else{this.zn=new vector();var q=this.begin;while(pos!=0){q=q.end;pos--;}this.zn.begin=q.begin;this.zn.end=q;this.zn.zn=buff;q.begin.end=this.zn;q.begin=this.zn;}} this.del_front=function(){if(this.begin!=null){this.zn=this.begin.end;delete this.begin;this.begin=this.zn;}} this.del_back=function(){if(this.begin!=null){this.zn=this.end.begin;delete this.end;this.end=this.zn;}} this.del=function(pos){if(pos<=0)this.del_front(buff);else if(pos>=this.size()-1)this.del_back(buff);else{this.zn=this.begin;while(pos!=0){this.zn=this.zn.end;pos--;}this.zn.begin.end=this.zn.end;this.zn.end.begin=this.zn.begin;delete this.zn;}} this.front=function(){if(this.begin!=null)return this.begin.zn;else return null;} this.back=function(){if(this.begin!=null)return this.end.zn;else return null;} this.insert=function(pos){if(pos<0)return null;this.zn=this.begin;while(pos!=0){if(this.zn==null)return null;else{this.zn=this.zn.end;pos--}}return this.zn.zn;} this.clear=function(){while(this.begin!=null){this.zn=this.begin.end;delete this.begin;this.begin=this.zn;}} } function matrix() { this.it=new vector(); this.line_size=function(){return this.it.size();} this.col_size=function(pos){return this.it.insert(pos).size();} this.add_line=function(pos){this.it.add(pos,new vector());} this.add_line_front=function(){this.it.add_front(new vector());} this.add_line_back=function(){this.it.add_back(new vector());} this.del_line=function(pos){if(this.it.insert(pos)!=null){this.it.insert(pos).clear();delete this.it.insert(pos);this.it.del(pos);}} this.del_line_front=function(){if(this.it.begin!=null){this.it.front().clear();delete this.it.front();this.it.del_front();}} this.del_line_back=function(){if(this.it.end!=null){this.it.back().clear();delete this.it.back();this.it.del_back();}} this.add_col=function(posx,posy,zn){this.it.insert(posx).add(posy,zn);} this.add_col_front=function(posx,zn){this.it.insert(posx).add_front(zn);} this.add_col_back=function(posx,zn){this.it.posx,posy.add_back(zn);} this.del_col=function(posx,posy){this.it.insert(posx).del(posy);} this.del_col_front=function(posx){this.it.insert(posx).del_front();} this.del_col_back=function(posx){this.it.insert(posx).del_back();} this.insert=function(posx,posy){return this.it.insert(posx).insert(posy);} this.clear=function(){while(this.it.begin!=null)this.del_line_front();} } тем самым мы получаем оператор двумерный массив Matrix() var kon=new matrix(); // создаем двумерный массив \\.............работаем с ним kon.clear(); // очищаем массив освобождая память delete kon; оператор matrix() имеет команды (функции) работа с массивом первого уровня: line_size() - возвращает количество строк (x.length) add_line(pos) - создает пустой массив 2 уровня в позицию pos (x[pos]=new Array()) add_line_front() - тоже что и add_line(pos), но добавляет в начале массива (x[0]=new Array()) add_line_back() - тоже что и add_line(pos), но добавляет в конец массива (x[x.length]=new Array()) del_line(pos) - удалить строку в позицию pos соответственно del_line_front() удалить в начале, del_line_back() удалить в конце работа с массивом второго уровня: col_size(pos) - возвращает количество значений в pos строке add_col(posx,posy,zn) - добавить значение zn в строку posx, в позицию posy ( x[posx][posy]=y ) add_col_front(posx,zn) - добавить значение zn в строку posx, в начало массива 2 уровня ( x[posx][0]=y ) add_col_back(posx,zn) соответственно добавить в конец массива del_col(posx,posy) - удалить из строки posx и значение posy del_col_front(posx) - удалить из строки posx в начале строки del_col_back(posx) - удалить из строки posx в конце строки и самое главное чтение значения insert(posx,posy) - читаем, изменяем значение posy в строке posx (x[posx][posy]=z) clear() - очищаем массивы |
Часовой пояс GMT +3, время: 03:03. |