I need to distinguish between a property having a null value, and a property not existing at all. I could use a map, but for performance reasons, I am trying to use a fixed size array.
In the array, I could use null to indicate when a property does not exist at all. But, for a property that does exist and is null valued, is there a standard way to represent it in an array?
I thought of keeping a static member, e.g.
class MyClass {
private static final Object NULL = new Object(); // null wrapper
private Object[] m_arr = new Object[10];
// 'i' represents the index of a property in the array
boolean exists(int i) {
return m_arr[i] != null;
}
Object value(int i) {
if( !exists(i) ) throw new NullPointerException(); // does not exist
if( m_arr[i] == NULL ) return null;
// ... handling for other data types ...
}
}
Another possibility for representing null might be an enum?
class MyClass {
...
enum Holder {
NULL
}
...
// to check for a null value use m_arr[i] == Holder.NULL
}