My question is close to this one but not quite the same.
I have an inherited (as in, I can’t/won’t change it) array of parameters in my class like so:
public double[] params;
The class utilises these parameters in complex ways, so I would prefer to have human-readable names for each element in the array. In C, you would do something like this
#define MY_READABLE_PARAMETER params[0]
I am also aware that in Java I could create a bunch of constants or an enumerator with attributes. Then, to access a parameter I’d have to type something like this:
params[MY_READABLE_PARAMETER]
This is acceptable but I would really like to omit the array name altogether. Is it possible?
If you have an array of doubles and each array element in a specific position has a definite meaning you should create a class instead.
If you need to deal with an existing
double[]from “outside” you could have a constructor and/or a method that takes an array as a parameter.Edit – to explain my comment
If the original parent class that this is a subclass of (the reason the double[] exists in the first place) has a getter for the array, that could be overridden in this class, building and returning the array when requested — e.g.
If the array is directly accessible from an instance of the parent class, without a getter, e.g.
myArray = parentInstance.paramsordouble d2 = parentInstance.params[2]then (1) that’s a bad design and (2) callers could change the array values out from under youparentInstance.params[1] = 0.0;