R
Roedy Green
I wrote a method of the form
String[] someMethod( ... )
IntelliJ complained this was a wicked thing todo.
Why? Because the caller could modify the String array it at would
wreck by copy inside SomeMethod.
So what do people do? They use Arrays.asList to convert to a List, an
java.util.Arrays$ArrayList.
But List still lets the user modify the field which in turn modify the
underlying array. We have gained nothing.
We have to use Collections.unmodifiableList which gives us a
Collections.unmodifiableList wrapper around our List that throws
UnsupportedOperationException when we attempt to write.
The catch is the set methods are still there. We don't find out about
the problem of modifying this list until run time.
String[] someMethod( ... )
IntelliJ complained this was a wicked thing todo.
Why? Because the caller could modify the String array it at would
wreck by copy inside SomeMethod.
So what do people do? They use Arrays.asList to convert to a List, an
java.util.Arrays$ArrayList.
But List still lets the user modify the field which in turn modify the
underlying array. We have gained nothing.
We have to use Collections.unmodifiableList which gives us a
Collections.unmodifiableList wrapper around our List that throws
UnsupportedOperationException when we attempt to write.
The catch is the set methods are still there. We don't find out about
the problem of modifying this list until run time.