Java tutorial
//package com.java2s; import java.lang.reflect.Array; public class Main { private static <T> T[] filterNull(T[] views) { int end = 0; int length = views.length; for (int i = 0; i < length; i++) { T view = views[i]; if (view != null) { views[end++] = view; } } if (end == length) { return views; } //noinspection unchecked T[] newViews = (T[]) Array.newInstance(views.getClass().getComponentType(), end); System.arraycopy(views, 0, newViews, 0, end); return newViews; } }