Java ArrayList Average averageInColumns(ArrayList lines)

Here you can find the source of averageInColumns(ArrayList lines)

Description

average In Columns

License

Open Source License

Declaration

static public ArrayList<String> averageInColumns(ArrayList<String> lines) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    static public ArrayList<String> averageInColumns(ArrayList<String> lines) {

        ArrayList<ArrayList<String>> valuesInColumns = valuesInColumns(lines);
        ArrayList<String> result = new ArrayList<String>();

        for (int i = 0; i < valuesInColumns.size(); i++) {
            ArrayList<String> values = valuesInColumns.get(i);
            Double sum = 0.0;//from  w  ww  .  j av a 2s  .  c  om
            for (String value : values) {
                try {
                    sum = sum + Double.parseDouble(value);
                } catch (Exception e) {
                    sum = null;
                    break;
                }
            }
            if (sum == null)
                result.add(sum + "");
            else
                result.add(sum / values.size() + "");
        }

        return result;
    }

    static public ArrayList<ArrayList<String>> valuesInColumns(
            ArrayList<String> lines) {

        ArrayList<ArrayList<String>> valuesInColumns = new ArrayList<ArrayList<String>>();

        for (int row = 0; row < lines.size(); row++) {
            String[] values = lines.get(row).replace("<,>", "<>")
                    .split(",");
            for (int i = 0; i < values.length; i++) {

                if (valuesInColumns.size() <= i) {
                    valuesInColumns.add(new ArrayList<String>());
                }

                valuesInColumns.get(i).add(values[i]);
            }
        }

        return valuesInColumns;
    }
}

Related

  1. average(ArrayList nums)
  2. average(ArrayList array)
  3. getAverage(ArrayList data)
  4. getAverage(ArrayList values)
  5. getAverageOfList(ArrayList values)