开发者

How to encapsulate an ArrayList in Java

开发者 https://www.devze.com 2023-02-03 17:34 出处:网络
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 i

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消