I have an array of varying length, and I’d like to read from a fixed position. If the position is out of bounds, I’d like to read a null, instead of throwing. I can of course do something like
if(theArray.length <= colNum){ result = null; }
else{ result = theArray[colNum]; }
but that seems kind of inelegant. I’d like to be able to make a one-liner or simple function call that acts like theArray[colNum] except returns null instead of throwing an out-of-bounds exception. Is there something like that I’m overlooking? Am I overthinking this?
You can use the ternary operator.
Here is a one-liner: