All Downloads are FREE. Search and download functionalities are using the official Maven repository.

utils.ArrayOperations Maven / Gradle / Ivy

The newest version!
package utils;
import java.util.List;


public class ArrayOperations
{

	/**
	 * Returns an array of doubles
	 */
	public static double[] toArray(final List array){

		if(array == null){
			throw new NullPointerException("Input List is null");
		}

		double[] data = new double[array.size()];

		for (int i = 0; i < array.size(); i++) {
			data[i] = array.get(i);
		}

		return data;

	}

	/**
	  * Returns the sum of the array elements
	  */
	public static  Double sum(List< Double > array){

		if(array == null){
			throw new NullPointerException("Input List is null");
		}

		double result = 0.0;

		for(Double element : array){
			result += element;
		}

		return result;
	}


	/**
	  * Returns the squared sum of the arraty elements
	  */
	public static  Double sumSqr(List< Double > array ){

		if(array == null){
			throw new NullPointerException("Input List is null");
		}

		double result = 0.0;

		for(Double element : array){

            result += ( element*element );
		}

		return result;
	}


    /**
      * Returns the maximum element in the List
     */
	public static Double max(List< Double > array){

		if(array == null){
			throw new NullPointerException("Input List is null");
		}

		Double result = array.get(0);
		
		for(Double element : array){
			
			if(element > result){
                result = element;
			}	
		}
		
		return result;
	}


    /**
     * Returns the minimum element in the array
     */
	public static Double min(List< Double > array ){

		if(array == null){
			throw new NullPointerException("Input List is null");
		}

		Double result = array.get(0);
		
		for(Double element : array){
			
			if(element < result){
                result = element;
			}	
		}
		
		return result;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy