11

tmp = [null, null, null, Mars, Saturn, Mars]执行操作后, 我有以下字符串数组 - 字符串数组在allSig[d3].split(" ");哪里allSig。null 值是数组中的空值。现在我想删除空值。为此,我正在使用

tmp[indexNumber] != null不工作并且给予真实;取 null 作为值。即使我使用“null”作为字符串也不起作用。

这个怎么去掉。

public static String[] removeElements(String[] allElements) {
    String[] _localAllElements = new String[allElements.length];

    for (int i = 0; i < allElements.length; i++)
        if (allElements[i] != null)
            _localAllElements[i] = allElements[i];

    return _localAllElements;
}
4

7 回答 7

37

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()]);
}
于 2013-03-13T09:27:41.183 回答
2
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);
}

或者

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;
}

工作正常...

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" });
}
于 2012-08-17T08:37:27.283 回答
2

您正在创建一个与原始数组大小相同的数组。所以它与原始数组相同,因为您复制非空值并且默认值为空。

做这个 :

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;
}
于 2012-08-17T08:30:08.320 回答
2

抽象@az 答案,这适用于任何类类型:

@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()));
}
于 2016-02-18T20:28:41.640 回答
1

如果您希望数组仅包含非空值(即结果数组将是["Mars", "Saturn", "Mars"]),那么我会将其视为一个两部分的问题。

首先,您必须确定新数组的大小。通过检查,很容易看出它是3,但您需要对它们进行计数才能以编程方式计算。你可以这样说:

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

其次,您需要创建一个具有该大小的新数组。然后您可以将所有非空元素放入您的新数组中,就像您在上面所做的那样。

// 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;

您可以将这两个组件组合为results方法的新内容。在此处查看完整的组合代码和实时示例输出。

于 2012-08-17T08:31:06.263 回答
0
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;
}
于 2012-08-17T08:49:18.513 回答
0

这是一个非常古老的问题,但这个 Java 8+ 解决方案可能对某人有用:

public static String[] removeElements(String[] allElements) { return Arrays.stream(allElements) .filter(Objects::nonNull) .collect(Collectors.toArray()); } 或者,如果您像我一样喜欢可读代码并且不希望静态方法妨碍您,您可以使用静态导入进一步简化它:

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

于 2018-03-19T18:02:59.637 回答