简体   繁体   English

获取Java中已排序数组的原始索引(Android)

[英]get original index of a sorted array in java(Android)

In my code, i got a List of elements to loop through and calculate some values, now: 在我的代码中,我得到了一个元素列表以循环遍历并计算一些值,现在:

        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

    } 

the lower the value in ratioArray[i] the better ratio i got;now i am stuck at locating the best ratio, i can do this: ratioArray [i]中的值越低,我得到的比率就越好;现在我被困在寻找最佳比率时,我可以这样做:

Arrays.sort(ratioArray);

but then how do i get the index back? 但是那我该如何找回索引呢? i have to make the min value point to it's size 我必须使最小值指向它的大小

Best way is to iterate through ratioArray and DO NOT use 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

    } 

After the above code put this, 在上面的代码之后,

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

And you will get your minindex 你会得到你的minindex

Is there a need to compute all ratios first and then sort them? 是否需要先计算所有比率然后对它们进行排序? I would compute the ratio in the for loop (as you do it now) and then check, if it is better than the best computed ratio till now. 我会在for循环中计算比率(如您现在所做的那样),然后检查它是否比到目前为止的最佳计算比率好。 If yes, store it (and its index) as bestRatio and bestRatioIndex and go on - if not, just go for the next loop. 如果是,请将其(及其索引)存储为bestRatio和bestRatioIndex并继续-否则,只需进行下一个循环即可。 After the loop, you have the best ratio and its index in the two variables. 循环之后,您将在两个变量中获得最佳比率及其索引。 You could even leave the loop then in-between, if you find an exact matching. 如果找到完全匹配的内容,您甚至可以离开循环,然后在两者之间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM