2

I guess an other way to ask the same question is how to know the number of null pointing elements in an array?

int[] arrayOfEffectiveSizeTen = new int[10];
// some black box in which the content of the array is changed
calculateAverage(arrayOfEffectiveSizeTen) //how can you do this for instance?

Do you really have to do something like this in a for loop with i < arrayOfEffectiveSizeTen.length?

arrayOfEffectiveSizeTen[i] == null ? nullPointingElement++ : elementInArray++;

I couldn't find any duplicate question. Which I found weird. In my particular case it's about Java.

Joop
  • 3,706
  • 34
  • 55
  • 1
    Depends on the language you're using. For example, C arrays don't have a length property and allow you to access memory outside the bounds of the array. C# arrays have a Length property. – Joe Nov 23 '14 at 09:11
  • Thank you for your comment, I totally forgot that I didn't use the Java tag this time. Though it's very interesting to hear about the differences in the main languages. – Joop Nov 23 '14 at 09:20
  • 4
    An `int` can not be null. And to store objects, arrays should, most of the time, not be used. Collections should be preferred. So, what's your question, since the one you asked doesn't make sense? – JB Nizet Nov 23 '14 at 09:22
  • Thank you for pointing that out. I thought arrays had null pointers in them if not declared otherwise. Now I see that it depends on the type of array. Still my question remains, how can this be done? Do you need to increment a variable every time you put something new in the array? Is that the only correct way if 0 is also a valid number as input to calculate the mean? – Joop Nov 23 '14 at 09:26
  • @Joop-No,you can simply overwrite the element of array by assigning values. You don't need to itertae through the loop! Also, you can put any value,including null,on selective type like String,etc.-though that would result in run-time exception. Check my answer below for more detail! – Am_I_Helpful Nov 23 '14 at 09:29
  • 2
    No. You use a List, that allows adding an arbitrary number of objects and doesn't have a fixed length like arrays have. Google for "Java collections tutorial". – JB Nizet Nov 23 '14 at 09:30
  • As already said: `int` can't be `null` and if you use arrays, but you are not sure about how many elements you put in: use `ArrayList` or some other `Collection`. Moreover, you shouldn't make any assumptions on the order of your elements. What happens if your array looks like this: `{"hello","world","this",null,"is","a","test"}` ? Here the array length is `7`, the number of elements is `6` but the `null` is at index `3`. So: counting `null` does not help. You could however surround your code with a `try..catch` for `NullPointerExeception`. – GameDroids Nov 23 '14 at 09:41
  • @GameDroids that last suggestion made me wince. That is truly horrible... – Boris the Spider Nov 23 '14 at 09:43
  • 1
    @BoristheSpider: didn't have enough characters left in the comment to say that it's the programmers job to make sure that there are no nulls in the array (otherwise he didn't think things through). And the catch-try is *not* a suggestion :) it's just "possible" – GameDroids Nov 23 '14 at 09:46

2 Answers2

2

In Java,as soon as you create an array,basically of primitive types like int,byte,etc. which you mentioned here, all the array elements are automatically initialised with the value 0. Hence, the talk of non-null elements end here.

All these elements don't point to null in case of primitive data-types,but in case of reference datatypes, these values point to null unless explicitly specified OR unless the coder forces it to do by assigning to some other parameters. Though,even that would result to run-time exception in Java. So,prevent the use of reference data-types in such way without initialisation as it'd result in NUllPointerException.

For reference datatypes,we do

String sarray[]={"Hello","World"};  
// see here array initialised,else would have produced NullPointerException
for(int i=0;i<sarray.length;array++){
   // do Something
}

Hence,for primitive datatypes in Java, we always do

for(int i=0;i<array.length;array++){
   // do Something
}

This will always probably be the way of accessing array elements in Java where by definition,all the elements will constitute an array and hence,

Effective size of Array in Java = Actual size of Array in Java
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 1
    The elements don't point to null because they can't, so the sentence about "unless explicitly specified" makes no sense. You can't assign `null` to an int, period. – RealSkeptic Nov 23 '14 at 09:28
  • But,you can assign null to a String array,which more or less is a preferred datatype! That I have mentioned in the comment on this question! @RealSkeptic – Am_I_Helpful Nov 23 '14 at 09:30
  • 1
    There was nothing about strings in neither the question nor the answer. Furthermore, the OP wanted to average the values in his array, which means he really wishes them to be numeric. – RealSkeptic Nov 23 '14 at 09:32
  • @RealSkeptic-There is as the OP has asked about the general behaviour of Java towards arrays! And also, you can see the comments on the question which include talks about `Colllections`---which are also not primitives... – Am_I_Helpful Nov 23 '14 at 09:34
  • @RealSkeptic-You are taking the answer other way around,but,in relaity,it's a more general description for all data-types,whether user-defined OR primitives! – Am_I_Helpful Nov 23 '14 at 09:35
2

In Java, you have to differentiate between primitive types and reference types.

When an array is initialized with new <sometype>[size], all its elements are set to a default value. For int, long, byte etc, the value is 0. So you can run an average function on such an array, and you will get an average of 0.

If the type is boolean, the initial value is false.

If the type is any object type (class), including the well-known types such as String, Integer, etc, the default values are all nulls.

So basically, when you create an array, if it's of a primitive type, then you can say, by your definition, that its length is its "effective size", because 0 or false are legitimate values.

If it's of a reference type, then when you create it, its effective size - by your definition - is zero, because there are no non-null elements.

However, using new is not the only way to initialize an array. You can, for example, use something like:

Integer[] intObjects = { 5, 17, 32 };

Note that this is an array of type Integer, which is a reference type, rather than int, the primitive type. If you had initialized it with new Integer[3] you would have had all nulls. But when you initialize it as I showed, it contains references to three Integer objects containing the numbers 5, 17 and 32 respectively. So you can say its effective size is the same as its length.

Note, however, that it's also legitimate to write something like:

Integer[] intObjects = { 5, null, 32 };

With all that in mind, the answer to the question "How do I know how many null values are in the array" is:

  1. If the array is of primitive type, there are no null values. It contains legitimate values, even if they are zero/false.
  2. If the array is of a reference type, and you initialized it with new, then all its elements are null.
  3. If the array is of a reference type, and you initialized it with {...}, or assigned values since its initialization, you have no way to know how many nulls are in it, except by looping and counting.
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79