How to encapsulate an ArrayList in Java
I am开发者_开发问答 trying to write a getter for an ArrayList in Java such that the list returned cannot be modified (ideally at compile time). I know there must be some simple way to do this but it is eluding me. Does anybody know how to do this?
Collections.unmodifiableList(...)
is simple solution. Better one would be to use ImmutableList
from google-guava library
You can use the Collections.unmodifiableList. As short example is available here.
精彩评论