Java ArrayList Max Max(ArrayList values)

Here you can find the source of Max(ArrayList values)

Description

Identifies the maximum numeric value in a list.

License

Open Source License

Parameter

Parameter Description
values Numeric values to test

Exception

Parameter Description
Exception an exception

Return

Maximum value

Declaration

public static double Max(ArrayList<Double> values) throws Exception 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.*;

public class Main {
    /** Identifies the maximum numeric value in a list.
     */*from  w  w  w .ja v a  2  s  . c om*/
     * @param values Numeric values to test
     * @return Maximum value
     * @throws Exception
     */
    public static double Max(ArrayList<Double> values) throws Exception {
        ArrayList<Double> values2 = new ArrayList<Double>();
        for (Double value : values)
            if (!value.equals(Double.NaN))
                values2.add(value);

        if (values2.size() == 0)
            throw new Exception("The list was empty, so Max could not be determined.");

        int indexOfMax = 0;

        for (int i = 1; i < values2.size(); i++) {
            Double value = values2.get(i);

            if (value > values2.get(indexOfMax))
                indexOfMax = i;
        }

        return values2.get(indexOfMax);
    }

    /** Adds the specified value to each element in the list.
     * @param list List of integer values
     * @param amountToAdd Amount to add to each element
     * return New list with modified values
     */
    public static ArrayList<Integer> Add(ArrayList<Integer> list, int amountToAdd) {
        ArrayList<Integer> newList = new ArrayList<Integer>();

        for (int i : list)
            newList.add(i + amountToAdd);

        return newList;
    }
}

Related

  1. getMaxEntryLength(ArrayList> dArr)
  2. getMaxOfArrayListInteger(List integerList)
  3. getMaxString(ArrayList labels, int currindx)
  4. getMaxValue(ArrayList values)
  5. Max(ArrayList> data)
  6. maxEltsInOneSetOfValues(ArrayList values)
  7. maxInColumns(ArrayList lines)
  8. maxIntValue(ArrayList ints)
  9. maxLength(ArrayList> a)