Location类,用于搜索二维数组的最大元素出现的位置和值

在这里插入图片描述

这个public static Location
locateLargest(double[][] a) 这个我居然没看懂他是一个返回值为Location类的方法,我还在疑惑咋这个方法的名字是个分段的。。。请教了其他人才知道返回Location这个类, 而且这个方法是static,不用创建实例就可以引用,十分方便。



package Experiment2;

 

public class Location {

   
public int row, col;

   
public double maxValue;

 

   
Location() {

 

    }

   
Location(int r, int c, double max) {

       
this.row = r;

       
this.col = c;

       
this.maxValue = max;

    }

   
public static Location locateLargest(double[][] a) {

       
double max = a[0][0];

       
int r,c;

       
r = c = 0;

       
for (int i = 0; i < a.length; i++) {

           
for (int j = 0; j < a[i].length; j++) {

                if (a[i][j] > max) {

          
         max = a[i][j];

                    r = i;

                    c = j;

                }

           
}

       
}

 

       
Location loc = new Location(r, c, max);

 

       
return loc;

    }

 

}

 

 

 

 

 

 

 

测试类

 

package Experiment2;

 

public class Test_Location {

   
public static void main(String[] args) {

       
double[][] a = {{1, 2, 3}, {8, 9, 9, 5}, {4, 3, 5, 7, 8}};

       
Location test = new Location();

       
test = Location.locateLargest(a);

       
System.out.println("最大元素是:" +
test.maxValue);

       
System.out.println("位置是:(" + test.row
+ "," + test.col + ")");

    }

}

 

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是具体的代码实现过程。 首先,在Qt的widget界面创建一个QuickWidget控件,用于显示QML界面: ```cpp // 创建 QuickWidget 控件 QQuickWidget *quickWidget = new QQuickWidget(this); // 加载 QML 界面 quickWidget->setSource(QUrl("qrc:/qml/main.qml")); ``` 接着,在主函数使用 QVariantList 生成一系列二维数组,并将其传递至QML界面: ```cpp // 创建 QVariantList 对象 QVariantList list; // 向 QVariantList 添加二维数组 QVariantList array1; array1 << QVariantList{10, 10} << QVariantList{20, 20} << QVariantList{30, 30}; list << QVariant::fromValue(array1); QVariantList array2; array2 << QVariantList{40, 40} << QVariantList{50, 50} << QVariantList{60, 60}; list << QVariant::fromValue(array2); // 向 QML 界面发送 QVariantList 对象 QObject *rootObject = quickWidget->rootObject(); QMetaObject::invokeMethod(rootObject, "setData", Q_ARG(QVariant, QVariant::fromValue(list))); ``` 最后,在QML界面,使用MapCircle元素绘制这些数据点: ```qml import QtQuick 2.12 import QtQuick.Controls 2.12 import QtLocation 5.12 Item { id: root width: 800 height: 600 // 接收从 C++ 代码传递过来的数据 function setData(data) { for (var i = 0; i < data.length; i++) { var array = data[i]; for (var j = 0; j < array.length; j++) { var point = array[j]; // 创建 MapCircle 元素 var circle = Qt.createQmlObject("import QtLocation 5.12; MapCircle {}", root); circle.center = QtPositioning.coordinate(point[0], point[1]); circle.radius = 100; circle.color = "red"; circle.border.width = 2; circle.border.color = "white"; } } } // 创建地图控件 Map { id: map anchors.fill: parent plugin: Plugin { name: "osm" } zoomLevel: 14 } } ``` 在setData()函数,遍历C++代码传递过来的二维数组,创建MapCircle元素,并设置其位置、颜色等属性。最后,在QML界面创建地图控件,用于显示这些数据点。 这样,我们就使用Qt和QML实现了将二维数组传递至QML界面,并用MapCircle元素在地图上绘制这些数据点的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值