//矩阵类
class Matrix {//将一个数组构建成一个矩阵,行Row、列Column
constructor(data,Row,Column){this.data=data||[];this.Row=Row;//行
this.Column=Column;//竖
}//根据行、列返回矩阵元素
getItem(r,c){return this.data[r*this.Column+c]||0;
}//根据行、列设置矩阵元素
setItem(r,c,item){this.data[r*this.Column+c]=item;
}//换行
swapRow(r1,r2){for(let c=0;c
const cache=this.getItem(r1,c)this.setItem(r1,c,this.getItem(r2,c))this.setItem(r2,c,cache);
}
}
oneRowEach(r,callback){for(let c=0;c
callback(this.getItem(r,c),r,c)
}
}//按行遍历矩阵元素,返回元素item,行r,列c
rowEach(callback){for(let r=0;r
callback(this.getItem(r,c),r,c)
}
}
}//按竖遍历矩阵元素,返回元素item,行r,列c
columnEach(callback){for(let c=0;c
callback(this.getItem(r,c),r,c)
}