Skip to content
Advertisement

How do I check if my int array is empty, with the standard value being 0?

So I have come across an issue I do not seem to be able to fix. So lets say I have an int array and want to check whether the array still has space to add a certain element (from 0-∞) or has no space left, which would mean I would need to create a new array.

The issue is that when my int array has a space to store ten values, all of the spaces are filled with 0, so my program thinks that this array is full. And I can not exclude 0 either because the element which I want to add could be 0 aswell.

Any advice?

Advertisement

Answer

You are probably using an int[]? The primitive type int can not be null. A very simple solution would be to use the wrapper class Integer.

Integer[] intArray = {null, 0, 10};
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement