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

  • Thread starter Thread starter nkk2008
  • Start date Start date
  • Tags Tags
    Java Value
Click For Summary
To trim an array of doubles in Java, it's important to understand that uninitialized indices in a double array default to 0, not null. Therefore, checking for null is not applicable for primitive types like double. Instead, to identify unfilled indices, use a comparison to 0. For example, iterate through the array and check if each element equals 0. If you need to represent the absence of a value more explicitly, consider using an array of Double objects, which can hold null values, but this will increase memory usage.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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