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

  • Context: Java 
  • Thread starter Thread starter nkk2008
  • Start date Start date
  • Tags Tags
    Java Value
Click For Summary
SUMMARY

The discussion addresses how to check for unfilled indices in a double array in Java. It clarifies that uninitialized elements in a double array default to 0, thus the correct approach is to check if the value at a given index equals 0. The user Nkk initially attempted to compare the array elements to null, which is not applicable for primitive types like double. Alternatives include using Double objects instead of primitive doubles to utilize null checks.

PREREQUISITES
  • Understanding of Java arrays and their default values
  • Familiarity with primitive types versus object types in Java
  • Knowledge of control structures, specifically for loops
  • Basic concepts of floating-point representation in programming
NEXT STEPS
  • Learn how to implement Java's Double class for nullable types
  • Explore Java's handling of primitive types and their default values
  • Research floating-point special values and their applications
  • Study best practices for array initialization and management in Java
USEFUL FOR

Java developers, software engineers, and students learning about array management and type handling in Java.

nkk2008
Messages
32
Reaction score
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
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.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 14 ·
Replies
14
Views
6K
Replies
20
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K