简体   繁体   中英

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.

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.

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
  2. Index returned after second call is 3, cos 2 is the next lowest value
  3. Index returned after second call is 2, cos 3 is the next lowest value and so on

Please could someone help me out. Thanks.

Your method returns an int which means it cannot return anything other ONE value. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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