Here you can find the source of maxMinValues(int[] array)
Parameter | Description |
---|---|
array | Input array |
public static int[] maxMinValues(int[] array)
//package com.java2s; /******************************************************************************* * Copyright (c) 2016 Pablo Pavon-Marino. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * * Contributors://ww w. j a v a 2 s .co m * Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1 * Pablo Pavon-Marino - from version 0.4.0 onwards ******************************************************************************/ import java.util.*; public class Main { /** * Returns the maximum/minimum values of an input collection. * * @param collection Input collection * @return Maximum/minimum values */ public static int[] maxMinValues(Collection<Integer> collection) { if (collection.isEmpty()) throw new NoSuchElementException("Empty collection"); int maxValue = Integer.MIN_VALUE; int minValue = Integer.MAX_VALUE; for (int value : collection) { if (value > maxValue) maxValue = value; if (value < minValue) minValue = value; } return arrayOf(maxValue, minValue); } /** * Returns the maximum/minimum values of an input array. * * @param <A> the map key type * @param map Input map * @return Maximum/minimum values */ public static <A> int[] maxMinValues(Map<A, Integer> map) { return maxMinValues(map.values()); } /** * Returns the maximum/minimum values of an input array. * * @param array Input array * @return Maximum/minimum values */ public static int[] maxMinValues(int[] array) { if (array.length == 0) { throw new NoSuchElementException("Empty array"); } int maxValue = array[0]; int minValue = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > maxValue) maxValue = array[i]; if (array[i] < minValue) minValue = array[i]; } return arrayOf(maxValue, minValue); } /** * Generates an {@code int[]} from comma-separated values. * * @param values Comma-separated {@code int} values * @return {@code int[]} */ public static int[] arrayOf(int... values) { return values; } }