Java Array Trim trimZeroTail(final double[] data)

Here you can find the source of trimZeroTail(final double[] data)

Description

Trim zero tail.

License

GNU General Public License

Parameter

Parameter Description
data the data

Return

the double[]

Declaration

public static double[] trimZeroTail(final double[] data) 

Method Source Code

//package com.java2s;
/*/*from   ww w.j  a  va2s.com*/
 * Title:        CloudSim Toolkit
 * Description:  CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2009-2012, The University of Melbourne, Australia
 */

import java.util.Arrays;

public class Main {
    /**
     * Trim zero tail.
     * 
     * @param data the data
     * @return the double[]
     */
    public static double[] trimZeroTail(final double[] data) {
        return Arrays.copyOfRange(data, 0, countNonZeroBeginning(data));
    }

    /**
     * Count non zero beginning of the data.
     * 
     * @param data the data
     * @return the int
     */
    public static int countNonZeroBeginning(final double[] data) {
        int i = data.length - 1;
        while (i >= 0) {
            if (data[i--] != 0) {
                break;
            }
        }
        return i + 2;
    }
}

Related

  1. trimBytes(byte[] source, int fromIndex, int toIndex)
  2. trimLeadingZeros(byte[] original)
  3. trimLeft(byte[] src, int trimSize)
  4. trimRecords(String[] records)
  5. trimStringArray(final String[] input)