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?

5
  • Will your array always be of type int[]?
    – Arc676
    Oct 29, 2015 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. Oct 29, 2015 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, 2015 at 14:00
  • 2
    Possible duplicate of stackoverflow.com/questions/2369967/…
    – Perdomoff
    Oct 29, 2015 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. Oct 29, 2015 at 14:03

6 Answers 6

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.

1
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

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;
1

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

Object array[] = new Object[size];
0

The solution that you want might be as follows. The method findEmptyIndex() will return the index of item with EMPTY value. If index -1 means array is 'full'. To safely store, use the storeValue() method.

public class Foo {
    public static final int EMPTY = -10000;   // assume the array will never 
                                              // store -10000 as item's value 

    public static int findEmptyIndex( int[] array ) {
        for ( int i = 0; i < array.length; i++ ){
            if( array[i] == EMPTY ){
                return i;
            }
        }
        return -1;
    }

    public static boolean storeValue( int[] array, int value ) {
       int avail = findEmptyIndex( array );
       if( avail > -1 ) {
           array[ avail ] = value;
           return true;   // succeed
       }
       return false;     // failed
    }

    public static void main( String[] args) {
       int[] array = new int[10];

       // initialize array
       for ( int i = 0; i < array.length; i++ ) {
          array[i] = EMPTY;
       }

       // after some times ...

       // to find available place and store value
       if( !storeValue( array, 199 ) ) {
           System.out.println("Value not stored, array's full");
       }
    }
}    

The array should be initialized with EMPTY before use so that the method findEmptyIndex() can check using comparison with the EMPTY.

3
  • The question is about the entire array being empty but your answer seems to only return the first null position. Your code also has the same problem as checking a primitive type (int) for null which is wrong in Java. Jan 25 at 1:37
  • The title is misleading. The question says "I need help to check if my array is full or if I can fit more in there". He wants to know if at least one place still empty /available, so i suggest findEmptyIndex(). If -1 then already full (no empty). If not -1 that means the position by the returned index is empty. To answer the is empty or not, just check if -1 (full) or not -1 (still got empty).
    – moyu-sharo
    Jan 26 at 2:11
  • "Your code also has the same problem as checking a primitive type (int) for null which is wrong in Java. " I'm not sure which one that you are referring to.
    – moyu-sharo
    Jan 26 at 2:14
-1
if(array.length == 0){
    //array is empty
}

should be enough

1
  • 2
    that would at best check of the length of my array was 0, not if it is anything in it
    – mynameishi
    Oct 29, 2015 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.