开发者

How do I print elements in an ArrayList without the null?

开发者 https://www.devze.com 2023-04-04 16:32 出处:网络
This is what I tri开发者_开发问答ed doing.. public void printArray() { for(int i = 0; i<size; i++)

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

精彩评论

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