简体   繁体   English

如何查找并返回数组中越来越低的值的索引 Java

[英]How to find and return the index of the increasingly lowest value in an array Java

I have an array { 8, 4, 3, 2, 5, 6, 1, 7 } which I want it to return the index of the lowest value in an array, the index of the next lowest value in the array and so on.我有一个数组{ 8, 4, 3, 2, 5, 6, 1, 7 }我希望它返回数组中最低值的索引,数组中下一个最低值的索引等等.

I have this code;我有这个代码;

private static int getSlotPosition(int[] iArray) {
        int lowestValue;
        lowestValue = iArray[0];
        int slot = 0;
        for (int i = 0; i < iArray.length; i++) {
            if (iArray[i] < lowestValue) {
                lowestValue = iArray[i];
                slot = i;
            }

        }
        return slot;

    }

The above method is being called in a for-loop, changing the arrangement of the array every time it is called.上面的方法是在for循环中调用的,每次调用都会改变数组的排列方式。

So I want the output (that is, the index) to be returned after every call to the method (assuming the positions remain the same) to be like;所以我希望在每次调用该方法(假设位置保持不变)后返回的输出(即索引)是这样的;

  1. Index returned after first call is 6, cos 1 is the lowest value第一次调用后返回的索引为6,cos 1为最小值
  2. Index returned after second call is 3, cos 2 is the next lowest value第二次调用后返回的索引是 3,cos 2 是下一个最低值
  3. Index returned after second call is 2, cos 3 is the next lowest value and so on第二次调用后返回的索引是 2,cos 3 是下一个最低值,依此类推

Please could someone help me out.请有人帮我。 Thanks.谢谢。

Your method returns an int which means it cannot return anything other ONE value.您的方法返回一个 int 这意味着它不能返回任何其他 ONE 值。 If you want solve the task, you need to do it with recursion, or make the method return an array with the lowest indexes in order.如果你想解决这个任务,你需要用递归来完成,或者让方法按顺序返回一个索引最低的数组。

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

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