1

The following code is supposed to read through some numbers and put ' <== Smallest number' next to the smallest. What's my problem here? It doesn't seem to be working! Every time it seems to assign the wrong number as the minimum.

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;

    }

}
0

7 Answers 7

1
if (numbers.get(findMin(numbers)) == i) { // If the 'smallest' index value is equal to i.

By calling numbers.get() you fetch the value of the slot at i.

Sign up to request clarification or add additional context in comments.

Comments

1

The variable min in findMin() is actually the index of the minimum number in n. Change this:

if (n.get(i) < min)

to:

if (n.get(i) < n.get(min))

Store the return value of findMin() before entering the for loop:

final int min_idx = findMin(numbers);
for (int i = 0; i < numbers.size(); i++) {
    if (min_idx == 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));
    }
}

Comments

0

You can use collection sort method for sorting an list. Documentation

After the sort first element will be the smallest one

Comments

0

Here is an improved, working sample (though without the Scanner inputs for easy testing):

public static void main(String[] args){
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(7);
    numbers.add(3);

    int minIndex = findMin(numbers);
    for(int i = 0; i < numbers.size(); i++){
        if(minIndex == 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 minValue = Integer.MAX_VALUE; // Get value at index position 0 as the current smallest.
    int minIndex = -1;
    for(int i = 0; i < n.size(); i++){
        if(n.get(i) < minValue){
            minIndex = i;
        }
    }
    return minIndex;
}

There was some confusion as to if the findMin method returned the minimum value, or the minimum index. It now returns the minimum index. findMin is also now called only once, not for every iteration in the loop, which is a little cleaner (and slightly faster).

Comments

0
if (n.get(i) < min) 

should be:

if (n.get(i) < n.get(min))

See it

Comments

0

Just change the following code and it will work for sure.

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

and 

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) < n.get(min))**
    {                  
      min = i;              
    }         
  }          
  return min;     
} 

Comments

0

You see the unexpected behavior because you don't obey the contract for List.get(). The method expects an index as argument and returns you the value. You should not compare the value returned by get() to an index.

Also, in your findMin() method you should initialize min to Integer.MAX_VALUE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.