Remove null values from string array

Hi ,I have a string array in a jsp page which I save some values inside. After I store the values I want to print only those who are not null. How can I do this? Is there a way to delete the null values?
[217 byte] By [Vag82a] at [2007-10-3 1:09:20]
# 1

Why not do a for loop with if statement.

put this into the JSP but change 'array' to you array name.

<%

//go through the array to check all the values

for(int i=0; i<array.length();i++){

//If array is null, nothing happen

if(array==null){

//leave here blank

}

//If array not null, then print value in a new line

else{

out.print(array+"<br>");

}

}

%>

Does that help?

fabrimaa at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Well this is what I have already done, but thank you. The problem is that the results after this loop has run have a lot of spce between them because of the null values.
Vag82a at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Why don't you use continue (to skip the null records) instead of giving space?
newentrya at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thank you but because I am new in programming what do you mean to use continue. Can you explain it a little bit further?Thank you
Vag82a at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

>Thank you but because I am new in programming what do you mean to use continue. Can you explain it a little bit further?

<%

//go through the array to check all the values

for(int i=0; i<array.length();i++) {

//If array is null, nothing happen

if(array==null){

//leave here blank; instead use continue like:

//this will skip the statements next to it, and increments the value of i in for loop and continues to execute the body of for loop.

//The same will be repeated till the last iteration.

continue;

}

//If array not null, then print value in a new line

else{

out.print(array+"<br>"); //don't change the logic here

}

}

%>

newentrya at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

If you want to delete the null values during preprocessing, do:

String[] array = {"value1", "value2", null, "value4", null};

String[] remove = {null};

List list = new ArrayList(Arrays.asList(array));

list.removeAll(new ArrayList(Arrays.asList(remove)));

array = (String[]) list.toArray(new String[list.size()]);

Or, better use List or Set all the time instead of String[]. The List and Set provides much more useful functions.

Otherwise, if you want to skip the null values during displaying, then indeed use continue; when value == null.

BalusCa at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thank You. :)That was helpful.Regards,jAY
jay786a at 2007-7-14 18:06:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...