Skip to content
Advertisement

Adding null values to arraylist

Can I add null values to an ArrayList even if it has a generic type parameter?

Eg.

JavaScript

If so, will

JavaScript

return 1 or 0?

If I can add null values to an ArrayList, can I loop through only the indexes that contain items like this?

JavaScript

Or would the for each loop also loop through the null values in the list?

Advertisement

Answer

Yes, you can always use null instead of an object. Just be careful because some methods might throw error.

It would be 1.

Also nulls would be factored in in the for loop, but you could use

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement