remove Int element from int array - Android java.lang

Android examples for java.lang:array element remove

Description

remove Int element from int array

Demo Code

import java.lang.reflect.Array;

public class Main{

    public static int[] removeInt(int[] cur, int val) {
        if (cur == null) {
            return null;
        }/*from w  w w.j  av  a 2  s  .c  om*/
        final int N = cur.length;
        for (int i = 0; i < N; i++) {
            if (cur[i] == val) {
                int[] ret = new int[N - 1];
                if (i > 0) {
                    System.arraycopy(cur, 0, ret, 0, i);
                }
                if (i < (N - 1)) {
                    System.arraycopy(cur, i + 1, ret, i, N - i - 1);
                }
                return ret;
            }
        }
        return cur;
    }

}

Related Tutorials