Removing unfilled values or null values from array of String in Java

前端 未结 7 1267
醉话见心
醉话见心 2020-12-16 02:22

I have following String Array tmp = [null, null, null, Mars, Saturn, Mars] coming after doing the operation - allSig[d3].split(\" \"); where

相关标签:
7条回答
  • 2020-12-16 02:55

    Abstracting @az answer, this applies to any class type:

    @SuppressWarnings("unchecked")
    public static <T> T[] clean(T[] a) {
        List<T> list = new ArrayList<T>(Arrays.asList(a));
        list.removeAll(Collections.singleton(null));
        return list.toArray((T[]) Array.newInstance(a.getClass().getComponentType(), list.size()));
    }
    
    0 讨论(0)
提交回复
热议问题