I'm trying to remove null values from a array that is converted from a csv string.
This is what i'm trying to do.
public class ArrayTest {
public static void main(String[] args) {
String commaSeparated = "item1,null,item1,null,item3";
String [] items = commaSeparated.split(",");
String[] result = removeNull(items);
System.out.println(Arrays.toString(result));
}
public static String[] removeNull(String[] items) {
ArrayList<String> aux = new ArrayList<String>();
for (String elem : items) {
if (elem != null) {
aux.add(elem);
}
}
return (String[]) aux.toArray(new String[aux.size()]);
}
}
I still get the output as below which still has the null values. Can you please point me what is wrong with this
[item1, null, item1, null, item3]
null.