Java Array Delete arrayDelete(final int[] arr, final int ind)

Here you can find the source of arrayDelete(final int[] arr, final int ind)

Description

array Delete

License

Open Source License

Declaration

public static int[] arrayDelete(final int[] arr, final int ind) 

Method Source Code

//package com.java2s;
/*/*from www  .j a  v a  2  s  .com*/
 *  Copyright (C) 2011-2014 Brian Groenke
 *  All rights reserved.
 * 
 *  This file is part of the 2DX Graphics Library.
 *
 *  This Source Code Form is subject to the terms of the
 *  Mozilla Public License, v. 2.0. If a copy of the MPL 
 *  was not distributed with this file, You can obtain one at 
 *  http://mozilla.org/MPL/2.0/.
 */

public class Main {
    /**
     * Deletes the object at the specified index of 'src' array by storing its
     * contents in 'dest' minus the index of deletion.
     * 
     * @param src
     * @param dest
     *            array to store new results in; the length MUST be src.length -
     *            1
     * @param ind
     * @return the dest array
     */
    public static <T> T[] arrayDelete(final T[] src, final T[] dest, final int ind) {

        if (dest.length != src.length - 1) {
            throw (new IllegalArgumentException("destination array length must equal source array length - 1"));
        }
        int offs = 0;
        for (int i = 0; i < src.length; i++) {
            if (i == ind) {
                offs = 1;
                continue;
            }
            dest[i - offs] = src[i];
        }
        return dest;
    }

    public static int[] arrayDelete(final int[] arr, final int ind) {

        int[] narr = new int[arr.length - 1];
        int offs = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == ind) {
                offs = 1;
                continue;
            }

            narr[i - offs] = arr[i];
        }
        return narr;
    }

    public static float[] arrayDelete(final float[] arr, final int ind) {

        float[] narr = new float[arr.length - 1];
        int offs = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == ind) {
                offs = 1;
                continue;
            }

            narr[i - offs] = arr[i];
        }
        return narr;
    }

    public static double[] arrayDelete(final double[] arr, final int ind) {

        double[] narr = new double[arr.length - 1];
        int offs = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == ind) {
                offs = 1;
                continue;
            }

            narr[i - offs] = arr[i];
        }
        return narr;
    }

    public static boolean[] arrayDelete(final boolean[] arr, final int ind) {

        boolean[] narr = new boolean[arr.length - 1];
        int offs = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == ind) {
                offs = 1;
                continue;
            }

            narr[i - offs] = arr[i];
        }
        return narr;
    }

    public static byte[] arrayDelete(final byte[] arr, final int ind) {

        byte[] narr = new byte[arr.length - 1];
        int offs = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == ind) {
                offs = 1;
                continue;
            }

            narr[i - offs] = arr[i];
        }
        return narr;
    }
}

Related

  1. arrayDelete(byte[] array, int length)