I have following String Array
tmp = [null, null, null, Mars, Saturn, Mars]
coming after doing the operation -
allSig[d3].split(\" \");
where
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());
}
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()]);
}
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;
}
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.
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" });
}
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;
}