gpt4 book ai didi

java - 在java中获取排序数组的原始索引(Android)

转载 作者:行者123 更新时间:2023-12-02 00:32:12 26 4
gpt4 key购买 nike

在我的代码中,我现在得到了一个要循环并计算一些值的元素列表:

        double targetRatio = Math.min((double)w/h, (double)h/w);//height & width of the screen
List<Size> sizes //populated with supported height and width
double ratioArray[];
int i;
for (i = 0; i <= sizes.size(); i++)
{
double ratio = Math.min((double)sizes.get(i).width/sizes.get(i).height, (double)sizes.get(i).height/sizes.get(i).width);
ratioArray[i] = Math.abs(ratio - targetRatio);
// Math.min((double)sizes.get(i).width/w, (double)w/sizes.get(i).width);
// Math.min((double)h/sizes.get(i).height, (double)sizes.get(i).height/h);
//sizes.get(i).width
//sizes.get(i).height

}

ratioArray[i] 中的值越低,我得到的比率就越好;现在我陷入寻找最佳比率的困境,我可以这样做:

Arrays.sort(ratioArray);

但是我如何取回索引呢?我必须使最小值指向它的大小

最佳答案

最好的方法是迭代ratioArray并且不要使用Arrays.sort(ratioArray);

double targetRatio = Math.min((double)w/h, (double)h/w);//height & width of the screen
List<Size> sizes //populated with supported height and width
double ratioArray[];
int i;
for (i = 0; i <= sizes.size(); i++)
{
double ratio = Math.min((double)sizes.get(i).width/sizes.get(i).height, (double)sizes.get(i).height/sizes.get(i).width);
ratioArray[i] = Math.abs(ratio - targetRatio);
// Math.min((double)sizes.get(i).width/w, (double)w/sizes.get(i).width);
// Math.min((double)h/sizes.get(i).height, (double)sizes.get(i).height/h);
//sizes.get(i).width
//sizes.get(i).height

}

上面的代码加上这个之后,

        int min = ratioArray[0];
int minindex;
for (int i = 0; i < ratioArray.length; i++) {
if(min > ratioArray[i]) {
min = ratioArray[i];
minindex = i;
}
}

你会得到你的 minindex

关于java - 在java中获取排序数组的原始索引(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8729842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com