Java Array Remove removeNulls(T[] array)

Here you can find the source of removeNulls(T[] array)

Description

remove Nulls

License

Open Source License

Declaration

public static <T> T[] removeNulls(T[] array) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

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

public class Main {
    public static <T> T[] removeNulls(T[] array) {
        for (T t : array) {
            if (t == null) {
                List<T> list = new ArrayList<>();
                for (T t2 : array)
                    if (t2 != null)
                        list.add(t2);//from   ww w.  ja v a2s .  co  m
                return list.toArray(Arrays.copyOf(array, list.size()));
            }
        }
        return array;
    }

    /** Add arrays
     * @param array1 An array to add to (or null)
     * @param array2 An array to add to (or null)
     * @return new array with contents of both arrays, or null if both arrays are null
     * @param <T> the array entry type
     */
    public static <T> T[] add(T[] array1, T[] array2) {
        if (array1 == null || array1.length == 0)
            return array2;
        if (array2 == null || array2.length == 0)
            return array1;

        T[] na = Arrays.copyOf(array1, array1.length + array2.length);
        System.arraycopy(array2, 0, na, array1.length, array2.length);
        return na;
    }
}

Related

  1. removeNaN(double[] x1)
  2. removeNodes(T[] array, T sampleNode)
  3. removeNull(T[] data)
  4. removeNullElements(String[] x)
  5. removeNulls(Object[] arr)
  6. removeNulls(T[] array)
  7. removeNulls(T[] input)
  8. removeNullsFromStringArray(String[] array)
  9. removeOwnCookieNameFromCookieHeader(String ownCookieName, String[] cookies)