Finding the min max and average of an array

南笙酒味 提交于 2019-12-25 16:09:09

问题


I need to find the min, max, and average of the balances. I have done this before using for loops, but never with a while loop. Is there a way to pull the min max and average straight from the array without interfering with the while loop perhaps?

10
Helene 1000
Jordan 755
Eve 2500
Ken 80
Andrew 999
David 1743
Amy 12
Sean 98
Patrick 7
Joy 14

where 10 is the number of accounts

import java.util.*;
import java.io.*;
import java.util.Arrays;

public class bankaccountmain {

public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = null;
    try {
        inFile = new Scanner(new File("account.txt"));
        ;
    } catch (FileNotFoundException e) {
        System.out.println("File not found!");

        System.exit(0);
    }
    int count = 0;
    int accounts = inFile.nextInt();
    String[] names = new String[accounts];
    int[] balance = new int[accounts];
    while (inFile.hasNextInt()) {
        inFile.next();
        names[count] = inFile.next();
        inFile.nextInt();
        balance[count] = inFile.nextInt();
        count++;
    }
    System.out.println(Arrays.toString(names));
    System.out.println(Arrays.toString(balance));
    }
}

回答1:


You are already pulling the values out of the file when you store them in balance[count]. You can then use those values you have just read to do your calculations:

    int min = Integer.MAX_VALUE;
    int max = 0;
    int total = 0;
    while (inFile.hasNext()) {
        names[count] = inFile.next();
        balance[count] = inFile.nextInt();  // balance[count] now is storing the value from the file

        if (balance[count] < min) {  // so I can use it like any other int
            min = balance[count];
        }
        if (balance[count] > max) {  // like this
            max = balance[count];
        }
        total += balance[count]; // and this
        count++;
    }
    double avg = (double)total/count;



回答2:


The answer of azurefrog is correct and acceptable (+1).

But I personally prefer to avoid "mixing" too much functionality. In this case: I think that

  • reading the values from a file and
  • computing the min/max/avg

should be separate operations.

Computing the min/max/average of an int[] array is a very common operation. So common that it probably already has been implemented thousands of times (and it's a pity that this functionality was not available in the standard API before Java 8).

However, if I had to implement something like this (and was not willing or allowed to use a sophisticated library solution like, for example, the SummaryStatistics of Apache Commons Math), I'd go for some utility methods:

static int min(int array[]) 
{
    int result = Integer.MAX_VALUE;
    for (int a : array) result = Math.min(result, a);
    return result;
}

int max(int array[]) 
{
    int result = Integer.MIN_VALUE;
    for (int a : array) result = Math.max(result, a);
    return result;
}

static int sum(int array[]) 
{
    int result = 0;
    for (int a : array) result += a;
    return result;
}

static double average(int array[]) 
{
    return (double)sum(array)/array.length;
}

Another advantage (apart from the "purists" argument of "Separation Of Concerns") is that these methods are reusable. You can put them into a IntArrays utility class, and whenever you need this functionality, you can just call these methods.

If you can already use Java 8, then it's even simpler, because exactly these operations now (finally!) are part of the standard API. They are offered in the IntStream class, that exactly has the min(), max() and average() methods that you need. There you could just write something like

int min = IntStream.of(balance).min().getAsInt();
int max = IntStream.of(balance).max().getAsInt();
double average = IntStream.of(balance).average().getAsDouble();


来源:https://stackoverflow.com/questions/23303225/finding-the-min-max-and-average-of-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!