class Location{
constructor(row,colum,maxValue){
this._row=row//列下标
this._colum=colum//行下标
this._maxValue=maxValue
}
get row(){
return this._row
}
get colum(){
return this._colum
}
get maxValue(){
return this._maxValue
}
set row(val){
this._row=val
}
set colum(val){
this._colum=val
}
set maxValue(val){
this._maxValue=val
}
display(){
console.log('属性组的最大值:',this._maxValue);
console.log('最大值的行下标',this._row);
console.log('最大值的行下标',this._colum);
}
}
//函数的功能:找出数组的最大值,用location类的对象保存数组组大值及其下标
//函数的参数:是数组
//函数的返回值:是location类的对象
function Locatelargest(arr) {
//1.创建location类的对象:假设二维数组的一一个元素为最大值
let max_locate=new Location(0,0,arr[0][0])
//遍历二位数组元素找出最大值
for(let i=0;i<arr.length;i++){//外循环:是数组的行下标
for(let j=0;j<arr[i].length;j++){//内循环:是数组的列下标
if(max_locate.maxValue<arr[i][j]){
max_locate.row=i
max_locate.colum=j
}
}
}
//3.返回location类的对象
return max_locate
}
let arr=[[34,35,35],[45,56,57]]
let locate=Locatelargest(arr)
locate.display()
设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括属性row(行下标)、column(列下标)和maxValue(最大值)。编写下面的函数,返回一个Location的对象(对象中包含了二维数组的最大值、最大值的行下标和列下标):
locateLargest(arr)