2

I'm writing a Java program with an object containing an array and indices first and last to represent positions in the array where values are defined. So for instance if a double array of length 8 is supposed to be blank except at the 3rd, 4th, and 5th coordinates, like [_, _, _, -1.0, 4.2, -5.9, _, _] then first will store 3 and last will store 6 (one more than the last position where values are defined). However, I don't know how to have null values inside a Java array. It initializes everything to 0.0 and I'm not able to assign null values to any coordinates.

I'm required to use arrays for this task and an array of all 0.0s is supposed to be regarded as full, so I can't just choose to interpret that as empty.

Thanks for any help.

Addem
  • 3,635
  • 3
  • 35
  • 58
  • 1
    Without having much background on what specific task you want to accomplish, you could use `OptionalDouble`. But, you probably would have more insight if you describe exactly your use-case. – Tunaki Jul 31 '16 at 22:39
  • 2
    The old answer is to use `Double[]` and `null`. The more modern answer is to use an `OptionalDouble[]` but I think that might be not what is asked for in your assignment. You could also use a `boolean[]` to mark if an item is present. – Boris the Spider Jul 31 '16 at 22:42
  • Are you declaring the array globally? Is it a data member or u define it in a method inside your class? – Andrei Mesh Jul 31 '16 at 22:42
  • It's hard to give more description. It ultimately will go into simulating some physics of vibrations in a class project, but the full description of that is long and complex and I'm not sure it will help answer the question. Fundamentally it's just supposed to be a certain sort of data structure with enqueue and dequeue methods--not sure I know how to effectively say more. – Addem Jul 31 '16 at 22:44
  • 2
    If all else fails, I like the idea of a boolean array that marks if a value is present. You could write a wrapper class around the double and boolean arrays. – OneCricketeer Jul 31 '16 at 22:45
  • @cricket_007 I agree, that sounds like a pretty good idea. – Addem Jul 31 '16 at 22:47
  • Instead of a boolean array, consider also a `BitSet` ([links](http://stackoverflow.com/questions/605226/boolean-vs-bitset-which-is-more-efficient)). – Tunaki Jul 31 '16 at 22:52
  • Does it matter what's in the slots in the array that are not between `first` and `last`? You can check if it's empty simply by checking if `first==last`, regardless of what the actual contents of the array are. And it is full is `last-first==array.length`. If nothing else, you could introduce a private `size` field. – James_D Jul 31 '16 at 23:36

3 Answers3

8

You can use null to represent "undefined" for reference types. A double cannot be null (primitive type, not a reference type), but a Double can be. So you can use a Double[] instead of a double[].

But are you sure you need to be concerned about "undefined" values? It seems to me that it will be the responsibility of your data structure to know that any index below first and above last contains an undefined value. That is, without knowing the actual values stored at such indexes. The actual values stored there could be anything, shouldn't matter. That's the impression I'm getting from the little you revealed about the exercise, I may be wrong.

janos
  • 120,954
  • 29
  • 226
  • 236
  • That's a great point, however, I need to be able to distinguish between a full and empty array, and both seem to be cases where `first==last`. So I've thought about that kind of approach to the problem but am not seeing how to distinguish full from empty arrays if I go that route. – Addem Jul 31 '16 at 22:49
  • 1
    I suppose that `first == last` is an empty array, whereas `last - first + 1== storage.length` should be the indicator of a full array. – janos Jul 31 '16 at 22:51
  • @Addem Reading the problem description, my interpretation was the same as janos'. The suggestion of a boolean array or bitset only makes sense if many "empty" values can be interleaved with valid values throughout the array. From your description though, `first` and `last` are sufficient, and it sounds like ignoring values outside that range will work fine. – erickson Jul 31 '16 at 23:24
1

If your array is of primitive type double[] then it can't be null. However Double[] (upper case) is an object wrapper for the primitive which allows null values.

Drgabble
  • 618
  • 5
  • 17
  • I was given a code skeleton that uses `double[]` so I don't think I have a choice about that. – Addem Jul 31 '16 at 22:46
0

If you must store the values in a double[] then there is not easy way to represent an unknown value. Any double value could theoretically be a valid values.

You have two options.

The first is to designate a specific double value to represent 'unknown'. For example you could use Double.NaN to represent an unknown value.

The second option is to store the values that are unknown in a separate structure. You could use boolean[] or Map<Integer,Boolean> or even List<Integer (to hold all the unknown indices).

sprinter
  • 27,148
  • 6
  • 47
  • 78