Java ArrayList Sum sumInColumns(ArrayList lines)

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

Description

sum In Columns

License

Open Source License

Declaration

static public ArrayList<String> sumInColumns(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> sumInColumns(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;// w  w w  . java  2  s  .  com
            for (String value : values) {
                try {
                    sum = sum + Double.parseDouble(value);
                } catch (Exception e) {
                    sum = null;
                    break;
                }
            }
            result.add(sum + "");
        }

        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. sum(ArrayList values)
  2. sum(ArrayList values)
  3. Sum(ArrayList values)
  4. sum(ArrayList a)
  5. sumVector(ArrayList v1, ArrayList v2)