IndexOf does not work in Arrays?

0

I have the following code that holds an array with 7 values, each for each day of the week. then I have to create a function that sums these 7 values (successfully completed), says what is the largest value of this Array (successfully completed) and then indicate what day that value occurred (is where the prolema is ). I continue to have a problem in int day = rainHours.indexOf (maxTempDay); where it says "can not find symbol - method indexOf (int)." I leave here the code thanks for help.

public static void main(String[] args)
{
    int [] rainHours=new int[]{1,3,0,0,6,3,8};
    int sum = rainInBusyDays(rainHours);
    System.out.println(sum);
    int maxValue=maxRain(rainHours);
    int maxTempDay=mostRainy(rainHours);
    int day=rainHours.indexOf(maxTempDay);
    System . out . println("O valor máximo é:" + maxValue + " no " + day + "º dia da semana ");

}

public static int rainInBusyDays(int[] rainH)
{
    int total = 0;
    for (int i =1; i<rainH.length-1 ;i++)
    {
        total =total + rainH[i];
    }
    return total;
}

public static int maxRain(int[] rainHo)
{
    int max = 0;
    for(int j=0;j<rainHo.length;j++)
    {
        if (rainHo[j] > max)
        {
            max= rainHo[j];
        }
    }
    return max;
}

public static int mostRainy(int[] rainHou)
{
    int maxTempDay = 0;
    for(int k = 0 ; k<rainHou.length;k++)
    {
        if(rainHou[k] > maxTempDay)
        {
            maxTempDay = rainHou[k];             
        }
    }
    return maxTempDay;
}
    
asked by anonymous 29.11.2017 / 19:23

1 answer

3

The indexOf method does not exist for arrays. If you want to know the int index in the array you can:

1 - Make a for in the array looking for the element (This function returns the day that had the maximum temperature, if it does not find that temperature it returns -1 )

public int maxTempDay(int maxTemp, int[] days) {
    for(int i = 0 ; i< days.length; i++) {
        if(maxTemp == days[i]) {
            //retorna o dia em que a temperatura estava máxima. 
            //Como o índice do array começa do zero você pode retornar i+1 para ter o dia;
            return i+1;
        }
    }
    // se terminar o for, não foi encontrado um dia com essa temperatura.
    return -1;
}

2 - Use the java.util.Arrays class to use the indexOf method of a List

java.util.Arrays.asList(rainHours).indexOf(maxTempDay);
    
29.11.2017 / 19:33