If statement returns index of only the first value meeting condition? Java

utopia :

I have been tasked with writing a class with a non-empty array 'temperatures' which stores the temperatures of 365 days of a year.

My task is to write a method returning the day of the year with the lowest temperature. For example, if the array temperatures = {0,0,-10,-10,0,......,0}. The corresponding result should be 3, despite there being two equal values with the lowest temperature of the set, as the 3rd day (second index) was the first to have the lowest value.

I have correctly written out the code, however, I'm unsure as to why, in my second If statement, it returns only the day with the lowest value and not all the days with the lowest value.

Here's my code

        public static int coldest(double[] temperatures) {
    double minimum = temperatures[0];
    for (double temp : temperatures) {
        if (temp < minimum) {
            minimum = temp;
        }
    }
    for (int i = 0; i < temperatures.length; i++) {
        if (Math.abs(minimum - temperatures[i]) < 0.000000001) {
            return i + 1;
        }
    }
    return -1;
}

For example, if I define double[] a = {-5,2,-5,2,-5,......2};, surely within the second For Loop, it will return 1,3,5,7... as all those days satisfy the If criteria, rather than just 1.

I apologise if I haven't written my question very clear, this is my first time asking here.

Arvind Kumar Avinash :

My task is to write a method returning the day of the year with the lowest temperature.

If it is the case, your logic is flawed e.g. the following condition doesn't make any sense:

if (Math.abs(minimum - temperatures[i]) < 0.000000001)

Do it as follows:

public class Main {
    public static void main(String args[]) {
        System.out.println(coldest(new double[] { 0, 0, -10, -10, 0, 0 }));
    }

    public static int coldest(double[] temperatures) {
        if (temperatures == null || temperatures.length == 0) {
            return -1;
        }
        double minimum = temperatures[0];
        for (double temp : temperatures) {
            if (temp < minimum) {
                minimum = temp;
            }
        }
        for (int i = 0; i < temperatures.length; i++) {
            if (minimum == temperatures[i]) {
                return i + 1;
            }
        }
        return -1;
    }
}

Output:

3

If your requirement is to get the list of all days with the minimum temperature, you need to return an array instead of a single value e.g.

import java.util.Arrays;

public class Main {
    public static void main(String args[]) {
        // Test
        System.out.println(Arrays.toString(coldestDays(new double[] { 0, 0, -10, -10, 0, 0 })));
    }

    public static int[] coldestDays(double[] temperatures) {
        if (temperatures == null || temperatures.length == 0) {
            return new int[0];
        }
        double minimum = temperatures[0];
        int count = 0;// To store the required size of the array
        for (double temp : temperatures) {
            if (temp < minimum) {
                minimum = temp;
            }
        }
        for (double t : temperatures) {
            if (t == minimum) {
                count++;
            }
        }
        int[] minTemps = new int[count];// Create the array
        int index = 0;
        for (int i = 0; i < temperatures.length; i++) {
            if (minimum == temperatures[i]) {
                minTemps[index++] = i + 1;// Store the (index +1 ) of the minimum temperatures
            }
        }
        return minTemps;
    }
}

Output:

[3, 4]

I am not sure if you have reached to the level of using Java collections. If yes, you can use an ArrayList and in that case, you won't need to find the count of days with the minimum temperatures first in order to create an array of appropriate size.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String args[]) {
        // Test
        System.out.println(coldestDays(new double[] { 0, 0, -10, -10, 0, 0 }));
    }

    public static List<Integer> coldestDays(double[] temperatures) {
        if (temperatures == null || temperatures.length == 0) {
            return new ArrayList<Integer>();
        }
        double minimum = temperatures[0];
        List<Integer> days = new ArrayList<Integer>();
        for (double temp : temperatures) {
            if (temp < minimum) {
                minimum = temp;
            }
        }

        for (int i = 0; i < temperatures.length; i++) {
            if (minimum == temperatures[i]) {
                days.add(i + 1);// Store the (index +1 )of the minimum temperatures
            }
        }
        return days;
    }
}

Output:

[3, 4]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344859&siteId=1