How do I check if there is nothing filling in the index at i of the array?

  • Java
  • Thread starter nkk2008
  • Start date
  • Tags
    Java Value
In summary, when an array of doubles has values that are not filled up, they are equal to 0. To check for empty values, you can use the "== null" test if the array is converted to Double objects. Otherwise, you can find a specific "illegal" value to use as an indicator for empty values.
  • #1
nkk2008
33
0
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:
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
 
Technology news on Phys.org
  • #2
When an array has values that are not filled up, they are equal to 0.

In other words:
Code:
//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.
 
  • #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.
 

Related to How do I check if there is nothing filling in the index at i of the array?

What is a double[] null value in Java?

A double[] null value in Java refers to an uninitialized or empty array of type double. This means that the array has been declared but does not contain any elements, and therefore has a null value.

How do I assign a null value to a double array in Java?

To assign a null value to a double array in Java, you can simply use the keyword "null" when declaring the array. For example: double[] myArray = null;

Can a null value be assigned to individual elements in a double array?

No, a null value cannot be assigned to individual elements in a double array. This is because a null value is used to indicate an empty array, and individual elements must have a value in order for the array to be considered initialized.

What happens when you try to perform operations on a double[] null value in Java?

If you try to perform operations on a double[] null value in Java, you will encounter a NullPointerException. This is because a null value indicates that the array does not have any elements, and therefore cannot perform any operations on them.

How can I check if a double array contains a null value in Java?

To check if a double array contains a null value in Java, you can use the Arrays class and its "equals" method. This method compares two arrays and returns true if they are equal. If you compare a double array to null and the result is true, then the array contains a null value.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top