append Int to int array - Android java.lang

Android examples for java.lang:array resize

Description

append Int to int array

Demo Code

import java.lang.reflect.Array;

public class Main{

    public static int[] appendInt(int[] cur, int val) {
        if (cur == null) {
            return new int[] { val };
        }/* w  w w . j a v  a2s.  co m*/
        final int N = cur.length;
        for (int i = 0; i < N; i++) {
            if (cur[i] == val) {
                return cur;
            }
        }
        int[] ret = new int[N + 1];
        System.arraycopy(cur, 0, ret, 0, N);
        ret[N] = val;
        return ret;
    }

}

Related Tutorials