Java Array Truncate truncate(int[] values, int value)

Here you can find the source of truncate(int[] values, int value)

Description

truncates the given array by removing all fields at the end that contain the given value, e.g., {0,1,0,2,3,0,0} => {0,1,0,2,3} for value=0

License

Open Source License

Parameter

Parameter Description
values array to truncate
value value to remove from the end of the array

Return

truncated array

Declaration

public static int[] truncate(int[] values, int value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  www  .  j  a  va 2  s .com
     * truncates the given array by removing all fields at the end that contain
     * the given value, e.g., {0,1,0,2,3,0,0} => {0,1,0,2,3} for value=0
     * 
     * @param values
     *            array to truncate
     * @param value
     *            value to remove from the end of the array
     * @return truncated array
     */
    public static int[] truncate(int[] values, int value) {
        if (values.length == 0) {
            return values;
        }
        if (values[values.length - 1] != value) {
            return values;
        }
        int index = values.length - 1;
        for (int i = values.length - 1; i >= 0; i--) {
            if (values[i] != value) {
                break;
            }
            index--;
        }
        int[] valuesNew = new int[index + 1];
        System.arraycopy(values, 0, valuesNew, 0, index + 1);
        return valuesNew;
    }

    /**
     * truncates the given array by removing all fields at the end that contain
     * the given value, e.g., {0,1,0,2,3,0,0} => {0,1,0,2,3} for value=0
     * 
     * @param values
     *            array to truncate
     * @param value
     *            value to remove from the end of the array
     * @return truncated array
     */
    public static double[] truncate(double[] values, double value) {
        if (values.length == 0) {
            return values;
        }
        if (values[values.length - 1] != value) {
            return values;
        }
        int index = values.length - 1;
        for (int i = values.length - 1; i >= 0; i--) {
            if (values[i] != value) {
                break;
            }
            index--;
        }
        double[] valuesNew = new double[index + 1];
        System.arraycopy(values, 0, valuesNew, 0, index + 1);
        return valuesNew;
    }

    /**
     * truncates the given array by removing all fields at the end that contain
     * the given value, e.g., {0,1,0,2,3,0,0} => {0,1,0,2,3} for value=0
     * 
     * @param values
     *            array to truncate
     * @param value
     *            value to remove from the end of the array
     * @return truncated array
     */
    public static long[] truncate(long[] values, long value) {
        if (values.length == 0) {
            return values;
        }
        if (values[values.length - 1] != value) {
            return values;
        }
        int index = values.length - 1;
        for (int i = values.length - 1; i >= 0; i--) {
            if (values[i] != value) {
                break;
            }
            index--;
        }
        long[] valuesNew = new long[index + 1];
        System.arraycopy(values, 0, valuesNew, 0, index + 1);
        return valuesNew;
    }
}

Related

  1. truncate(byte[] array, int newLength)
  2. truncate(byte[] element, int length)
  3. truncate(byte[] nextPartBytes, int partSize)
  4. truncate(final byte[] arr, final int len)
  5. truncate(int[] array, int maxLen)
  6. truncate(short[] value, int maxSize)
  7. truncateAndConvertToInt(long[] longArray)
  8. truncateBytes(byte[] inputBytes)
  9. truncateByteSegment(byte[] byteSegment, int toSize)