Here you can find the source of truncate(int[] values, int value)
Parameter | Description |
---|---|
values | array to truncate |
value | value to remove from the end of the array |
public static int[] truncate(int[] values, int value)
//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; } }