We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › use of null with arrays
Page Index Toggle Pages: 1
use of null with arrays (Read 805 times)
use of null with arrays
May 20th, 2010, 11:31am
 
So, I understand that null is not a value and just means the target doesn't exist, but I need to use something like it to reference empty elements of an array.  I need to do something like this:

for(int i=0; i < array1.length; i++){
 if(array1[i] != null){
   if(array2[i] == something){
     array1[i] = null;
   }
 }
}

In other words, I need to check if elements of the array are undefined as well as delete elements.  Setting them equal to 0 won't work for my purposes.  Any ideas?

Thanks,
Zach

Re: use of null with arrays
Reply #1 - May 20th, 2010, 12:45pm
 
It can work if it is an array of objects.
If you store ints or floats in this array, and that you need negative values (for example) too, you can make an array of Integer or Float objects. A bit more expansive in memory and slower, but unless the array is very big, it shouldn't be a problem.

An alternative is to store either Integer.MAX_VALUE (or MIN_VALUE), supposing you never reach such value; or Float.NaN:
float[] f = new float[5];
f[3] = Float.NaN; // Not a Number
println(f);
Page Index Toggle Pages: 1