11

操作を実行した後、次の文字列配列 tmp = [null, null, null, Mars, Saturn, Mars]があります- allSig[d3].split(" ");ここallSigで、は文字列の配列です。null値は、配列内の空の値です。ここで、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

最も簡単な解決策:

 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

元の配列と同じサイズの配列を作成しています。したがって、null以外の値をコピーし、デフォルト値がnullであるため、元の配列と同じです。

これを行う :

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

@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

配列にnull以外の値のみを含める場合(つまり、結果の配列は["Mars", "Saturn", "Mars"])、これを2つの部分の問題と見なします。

まず、新しい配列のサイズを特定する必要があります。検査から、それがであることが簡単にわかり3ますが、これをプログラムで計算するには、それらを数える必要があります。あなたは言うことによってこれを行うことができます:

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

次に、そのサイズの新しいアレイを作成する必要があります。次に、上記のように、null以外のすべての要素を新しい配列に配置できます。

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

メソッドの新しいコンテンツとして、これら2つのコンポーネントを組み合わせることができます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

これは非常に古い質問ですが、このJava8以降のソリューションは誰かに役立つかもしれません。

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 に答える