2

I need help to check if my array is full or if I can fit more in there. I thought I could check if just one of the positions was empty by doing the following:

    for (int i =0; i <array.length; i++){
        if(array[i] == null){
            return false;
        }else{
            return true;
        }
    }

To test this I created am array with int and got the error: The operator == is undefined for the argument type(s) int, null is there another way I can do this? In my original task I will do this with objects and not int, does this mean it will work with objects but not with int?

Suraj Bajaj
  • 6,630
  • 5
  • 34
  • 49
mynameishi
  • 59
  • 7
  • Will your array always be of type `int[]`? – Arc676 Oct 29 '15 at 13:59
  • 3
    primitive type can never be null hence you get that error. Yes it will work with object and not with int. For testing use Integer and not int. you will be able to check for null then. – StackFlowed Oct 29 '15 at 13:59
  • Why would you put a null into an array? If they are ints then use Integer that can be null and auto casts to int – Ya Wang Oct 29 '15 at 14:00
  • 2
    Possible duplicate of http://stackoverflow.com/questions/2369967/how-we-check-for-null-array-in-java – Perdomoff Oct 29 '15 at 14:01
  • 1
    @StackFlowed it doesn't work for objects either, because it only ever checks the first element of the array and returns immediately. – Erwin Bolwidt Oct 29 '15 at 14:03

5 Answers5

5

Primitive types cannot be null, that's why that does not work. With objects, it will work. In this case, you can use the following code:

public boolean isArrayFull(Integer[] array) {
    for (Integer i = 0; i < array.length; i++) {
        if (array[i] == null) {
            return false;
        }
    }
    return true;
}

This also fixes a bug in your code. Your code would return true, if the first element is not null.

ruizpauker
  • 384
  • 7
  • 19
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
1

There is no such thing as an "empty" element in a Java array.

So if it's an array of Object you will test with " == null "

but if it's an array of a primitive type and you did not assigned anything to that location, then it will have the value zero,.

So unless you are sure 0 will never be a "valid" value, you cannot just insert in the array at an "empty" location.

see : https://stackoverflow.com/a/286167/4088809

Community
  • 1
  • 1
Tahar Bakir
  • 716
  • 5
  • 14
1

Theres is another question with the answer for yours.

How to check if array indexes are empty and if so check the next? The answer there is:

 for(int i=0;i<newData.length;i++)
    {
        if(newData[i]==0)
            System.out.println("The value at " + i + "is empty");
    }

Optimized for your code(I think you want to to a method like isArrayFilles or something like that):

 for(int i=0;i<newData.length;i++)
    {
        if(newData[i]==0)
            return false;
    }

return true;
Community
  • 1
  • 1
Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
1

Try declaring your array as an object rather than a traditional array. EX:

Object array[] = new Object[size];
DrakePHP
  • 15
  • 6
-1
if(array.length == 0){
    //array is empty
}

should be enough

meda
  • 45,103
  • 14
  • 92
  • 122