开发者

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]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜