简体   繁体   中英

How do I write a recursive method that takes as an input an ArrayList of Integers and returns the smallest integer?

This is regularly how it's done but it has to be recursively with no for, do-while, and while loops. if statements only.

import java.util.ArrayList;
import java.util.Scanner;

public class arrayex1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        System.out.println("Enter numbers: ");

        for (int i = 0; i < 10; i++) {
            int num = input.nextInt();
            numbers.add(num);
        }

        for (int i = 0; i < numbers.size(); i++) {
            if (numbers.get(findMin(numbers)) == i) { // If the 'smallest' index value is equal to i.
                System.out.println(numbers.get(i) + " <== Smallest number");
            } else {
                System.out.println(numbers.get(i));
            }
        }
    }

    public static int findMin(ArrayList<Integer> n) {

        int min = 0; // Get value at index position 0 as the current smallest.

        for (int i = 0; i < n.size(); i++) {
            if (n.get(i) < min) {
                min = i;
            }
        } 

        return min;
    }
}

One way you could code it:

findMin should return int, take ArrayList<Integer> integers, int min, int index and be called with (integers, Integer.MAX_VALUE, 0).

findMin should check to see if the value of integers[index] is smaller than min - if it is, it updates min.

Then, if it is not at the last index in integers, it'll return the value of calling itself with (integers, min, ++index).

If it is, it will return min.

Here you go ...

public static void main(String[] args) throws Exception {

    final List<Integer> numbers = new ArrayList<Integer>() {
        {
            add(3);
            add(4);
            add(6);
            add(1);
            add(9);
        }

    };

    final int min = findSmallest(numbers.iterator(), Integer.MAX_VALUE);
    System.out.println("Smallest: " + min);
}

private static int findSmallest(Iterator<Integer> iterator, Integer max) {

    int min = Math.min(iterator.next(), max);
    if (iterator.hasNext()) {
        min = findSmallest(iterator, min);
    }

    return min;
}

You could do something like this.

int min = 2876529394; // Holds the smallest element. Put a number that you know won't 
                      // be in the ArrayList just to make the code simpler. If you don't
                      // have such a number, just implement a counter variable.

findMin( numbers, 0 );
public void findMin( ArrayList<Integer> a, int index ) {

    if( index < a.size() ) {

        if( a.get( index ) < min )
             min = a.get( index );

       findMin( a, ++index );

   }
}            

Here, you're essentially doing the exact same thing a for-loop would do in principle, but instead you're using recursion.

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