Java Array Remove removeElement(final E[] array, final int index)

Here you can find the source of removeElement(final E[] array, final int index)

Description

remove Element

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <E> E[] removeElement(final E[] array, final int index) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    @SuppressWarnings("unchecked")
    public static <E> E[] removeElement(final E[] array, final int index) {
        final List<E> list = new ArrayList<>(Arrays.asList(array));
        list.remove(index);//from w w  w  .  j ava  2s .c  om
        return list.toArray((E[]) new Object[list.size()]);
    }

    public static int[] removeElement(final int[] input, final int index) {
        final List<Integer> result = new ArrayList<>();

        for (int i = 0; i < input.length; i++) {
            if (i != index) {
                result.add(input[i]);
            }
        }

        return toIntArray(result);
    }

    public static int[] toIntArray(final List<Integer> list) {
        final int[] ret = new int[list.size()];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = list.get(i);
        }
        return ret;
    }
}

Related

  1. RemoveArgs(String[] args, int startIndex)
  2. removeAt(T[] array, int index)
  3. removeByPrefix(String[] array, String prefix)
  4. removeByteOrderMark(final byte[] input)
  5. removeCommonWords(String[] words)
  6. removeElement(Object[] array, Object entryToRemove)
  7. removeElement(String array[], String element, int occurrences)
  8. removeElement(String[] array, String remove)
  9. removeElement(String[] elements, String element)