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

前端 未结 7 1266
醉话见心
醉话见心 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:41

    This is a very old question but this Java 8+ solution might be useful to someone:

    public static String[] removeElements(String[] allElements) { return Arrays.stream(allElements) .filter(Objects::nonNull) .collect(Collectors.toArray()); } or if you, like me, are a fan of readable code and don't want static methods to get in your way, you can simplify this even further using static imports:

    public static String[] removeElements(String[] allElements) { return stream(allElements).filter(Objects::nonNull).collect(toArray()); }

    0 讨论(0)
  • 2020-12-16 02:43

    Simplest Solution :

     public static String[] clean(final String[] v) {
        List<String> list = new ArrayList<String>(Arrays.asList(v));
        list.removeAll(Collections.singleton(null));
        return list.toArray(new String[list.size()]);
    }
    
    0 讨论(0)
  • 2020-12-16 02:44
    public static String[] removeElements(String[] allElements) {
        String[] _localAllElements = new String[allElements.length];
        int j = 0;
        for (int i = 0; i < allElements.length; i++)
            if ( allElements[i] != null && !allElements[i].equals(""))
                _localAllElements[j++] = allElements[i];
    
        return _localAllElements;
    }
    
    0 讨论(0)
  • 2020-12-16 02:47

    If you want the array to contain only the non-null values (i.e. the resulting array would be ["Mars", "Saturn", "Mars"]), then I would look at this as a two part problem.

    First, you must identify what the size of the new array should be. From inspection, it's easy to see that it's 3, but you will need to need to count them to calculate this programmatically. You can do this by saying:

    // Calculate the size for the new array.
    int newSize = 0;
    for (int i = 0; i < allElements.length; i++)    {
        if (allElements[i] != null) {
            newSize++;
        }
    }
    

    Secondly, you will need to create a new array with that size. Then you can put all of the non-null elements into your new array, as you have done above.

    // Populate the new array.
    String[] _localAllElements = new String[newSize];
    int newIndex = 0;
    for (int i = 0; i < allElements.length; i++) {
        if (allElements[i] != null) {
            _localAllElements[newIndex] = allElements[i];
            newIndex++;
        }
    }
    
    // Return the new array.
    return _localAllElements;
    

    You can just combine these two components as the new content of your results method. See the full combined code and live sample output here.

    0 讨论(0)
  • 2020-12-16 02:50
    public static String[] clean(final String[] v) {
      int r, w;
      final int n = r = w = v.length;
      while (r > 0) {
        final String s = v[--r];
        if (!s.equals("null")) {
          v[--w] = s;
        }
      }
      return Arrays.copyOfRange(v, w, n);
    }
    

    or

    public static String[] clean(final String[] v) {
      int r, w, n = r = w = v.length;
      while (r > 0) {
        final String s = v[--r];
        if (!s.equals("null")) {
          v[--w] = s;
        }
      }
      final String[] c = new String[n -= w];
      System.arraycopy(v, w, c, 0, n);
      return c;
    }
    

    Works fine...

    public static void main(final String[] argv) {
      final String[] source = new String[] { "Mars", "null", "Saturn", "null", "Mars" };
      assert Arrays.equals(clean(source), new String[] { "Mars", "Saturn", "Mars" });
    }
    
    0 讨论(0)
  • 2020-12-16 02:51

    You're creating an array with same size as the original one. So it's the same as the original array, as you copy non null values and default values are null.

    Do this :

    public static String[] removeElements(String[] allElements) {
        // 1 : count
        int n = 0;
        for (int i = 0; i < allElements.length; i++)
            if (allElements[i] != null) n++;
    
        // 2 : allocate new array
        String[] _localAllElements = new String[n];
    
        // 3 : copy not null elements
        int j = 0;
        for (int i = 0; i < allElements.length; i++)
            if (allElements[i] != null)
                _localAllElements[j++] = allElements[i];
    
        return _localAllElements;
    }
    
    0 讨论(0)
提交回复
热议问题