Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java double[] null value

  1. Sep 26, 2010 #1
    I have an array of doubles that is too large (i.e. it is of length 10, but only indices 0 to 6 are filled).

    I want to trim it, and am having problems with the line

    Code (Text):
    for loop{
    if(array[i] == null{//THIS LINE
    action
    }
    }
    It says that "the argument type == is undefined for types double, null". So...what do I do? How do I check if there is nothing filling in the index at i of the array?

    Thanks,
    Nkk
     
  2. jcsd
  3. Sep 26, 2010 #2

    MysticDude

    User Avatar
    Gold Member

    When an array has values that are not filled up, they are equal to 0.

    In other words:
    Code (Text):

    //lets say "nums" is a double array with elements 0-6 filled up and the rest are left as you say
    for(int i = 0; i < nums.length; i++){
      if(nums[i] == 0){//this is the correct check, because in arrays, things that you don't set as whatever you want are, by default, set to zero
        //some action
      }
    }
     

    I can show this to you in another way.

    double nums[] = new double[10];
    By just leaving this as is, the array looks like this:

    0,0,0,0,0,0,0,0,0,0.
     
  4. Sep 27, 2010 #3
    Java is a hybrid of base types and Object types. The base "double" type is -- most probably -- exactly the underlying floating point hardware representation of a double value, just like in good'ole'C. On the other hand "Double" types are real Java objects wrapped around regular doubles. A "null" Java Object is represented as an empty reference -- most probably again -- just like you would have a null==0 address in a list of C pointers, but there is no concept of a null double base type.

    Floating Point has values that are "illegal" so you might find one of them that you can use as an "empty" indicator. Or you can convert your array to be Double objects -- which will take up a bit more space -- and use the "== null" test for existence.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook