How do I print elements in an ArrayList without the null?
This is what I tri开发者_开发问答ed doing..
public void printArray()
{
for(int i = 0; i<size; i++)
{
System.out.println("arr[" + i + "] = " + arr[i]);
}
}
this is what I get:
-arr[0] = Papa Bill
-arr[1] = Ali Baba
-arr[2] = Ming the Merciless
-arr[3] = Grandpa
-arr[4] = Tornado
-arr[5] = null
I need it to print with out null...
Just add a check for null in your for loop:
if ( arr[i] != null ){
System.out.println("arr[" + i + "] = " + arr[i]);
}
精彩评论