tags:

views:

137

answers:

5

I have a standard java array, where null values are used for unassigned or empty spots in the array. How can I tell if there are unassigned values left, i.e. if the array is not full?

+5  A: 

As an array as a static size, you could say that an array is always full.

But if you want to know how many null values are in your array, you simply have to go through it.

Object[] array = new Object[10];
int count = 0;
for(Object item : array){
    if(item == null)
        count++;
}

Or in a specific method :

public int countNulls(Object[] array){
    int count = 0;
    for(Object item : array){
        if(item == null)
            count++;
    }
    return count;
}

And if you're filling you array index by index :

public int nextEmptyIndex(Object[] array){
    int count = 0;
    for(Object item : array){
        if(item == null)
            return count;
        else
            count++;
    }
    return -1; //Return an invalid value or do something to say that there is no empty index.
}
Colin Hebert
okay then how do i find out if there is space in the array that is free
Luron
`if (countNulls(array) > 0) ...`
matt b
Your answer is misleading as it is only valid under two assumptions: that he has an object collection and the that values he is assigning are never null.
tau-neutrino
@tau-neutrino String[] can be used as Object[] and my first statement is that arrays are de facto "full" so of course it could be misleading and I make assumptions but if he fills its array will null values I can't figure out if these null are "assigned" nulls or "originals" null.
Colin Hebert
@Colin Hebert We're arguing the same point :-) It's just a bad question all around.
tau-neutrino
A: 

if(myarray[myarray.length-1] != null){
System.out.println("Array is full");
}

mezzie
This doesn't work for int[].
gawi
yah i guess it doesn't. the question was not very clear also sorry
mezzie
+2  A: 

If "not full" meaning there are null elements in the array, then you would create a method such as

public boolean isFull(Object[] array) {
   for (Object element : array)
      if (element == null) return false;
   }
   return true;
}
Codemwnci
A: 

While I understand your question, I'm a little confused why you are asking it. First, I'll take a stab at the Q itself.

Arrays in Java are of fixed size, so the concept of them being 'full' doesn't make a whole lot of sense. For primitive arrays, the whole array is initialized with the default value for the primitive. Therefore, if you know that the values you are assigning are non-default values, you can walk in from element length-1 checking for default values.

If you have an array of objects, you can check for nulls. Again, this assumes that you aren't assigning nulls at some point in your population (something that is arguably bad practice). Again, you can walk in from the last element of the array, checking as you go.

You can avoid this issue by going with a proper object collection, such as an array list. You can populate as many elements as you need.

tau-neutrino
Let me add one other point, since there really aren't true primitive arrays in Java as there are in lower level languages, you really don't gain anything performance-wise by going not to an object collection. Granted, the memory overhead will be larger with a collection of objects due to the slight overhead for maintaining the objects themselves. That really only becomes a factor when you have large collections though.
tau-neutrino
For some reason, many Java beginners uses arrays. There must be a lot of learning Java curriculum that advocates arrays.
Steve Kuo
Funny aside - I worked on a large international space mission with scientists from world renowned academic institutions. During a code review, I came across this multi-dimensional double array that was present all over the code. I kept analyzing how it was used, trying to understand the algorithm that it was being used in, but I just couldn't for the life of me figure it out.Eventually, I realized he was using the array to simulate memory. Yes, let me repeat that, he created a gigantic array that he passed around everywhere to simulate memory.It was actually quite insightful on some level
tau-neutrino
+1  A: 

Arrays always full, so

public boolean isFull(Object[] array) {
   return true;
}
Stas
alright we get it. i worded it wrong. relax
Luron
well, then you should fix the wording.
Erick Robertson