/** * Get the total value for the index. All indexes must be numbers. The list must be a list of * String arrays. * * @param list List: the list to average * @param precision int: the result decimal precision * @return String[]: the averages as strings */ public static String[] averageAll(List list, int precision) { String[] tmp = (String[]) list.get(0); String[] res = new String[tmp.length]; double[] d = new double[tmp.length]; for (int i = 0; i < d.length; i++) { d[i] = 0.0; } int size = list.size(); for (int i = 0; i < size; i++) { String[] sa = (String[]) list.get(i); for (int j = 0; j < sa.length; j++) { if (!sa[j].equals("")) { d[j] += Double.parseDouble(sa[j]); } } } NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(precision); nf.setMinimumFractionDigits(precision); nf.setMinimumIntegerDigits(1); for (int i = 0; i < d.length; i++) { res[i] = nf.format(d[i] / size); } return res; }
/** * Get the average value for the index. The list must be a list of String arrays. * * @param list List: the list to average * @param index int: the index to average * @param precision int: the result decimal precision * @return String: the average as a string */ public static String average(List list, int index, int precision) { double d = 0.0; int size = list.size(); for (int i = 0; i < size; i++) { String[] sa = (String[]) list.get(i); if (!sa[index].equals("")) { d += Double.parseDouble(sa[index]); } } NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(precision); nf.setMinimumFractionDigits(precision); nf.setMinimumIntegerDigits(1); return nf.format(d / size); }