I have writen some config data in a List and have to immutable reference, how? or create a clone copy?
g*g
2 楼
You can have your list as a field in another class, say ImmutableList, and in this class you only expose method that you want to expose. e.g. only the get(int i) class ImmutableList { private List data; public Object get(int i) { return data.get(i); } }
【在 p***p 的大作中提到】 : I have writen some config data in a List and have to immutable reference, : how? or create a clone copy?
p*p
3 楼
Well, I have to reture a List object, so any suggestion?
【在 g*****g 的大作中提到】 : You can have your list as a field in another class, : say ImmutableList, and in this class you only expose : method that you want to expose. e.g. only the get(int i) : class ImmutableList { : private List data; : public Object get(int i) { : return data.get(i); : } : }
【在 p***p 的大作中提到】 : Well, I have to reture a List : object, so any suggestion?
p*p
5 楼
是不是还加个implements? 还是extends ArrayList,然后再overload class ImmutableList implements List{ private List data; public Object get(int i) { return data.get(i); } }
【在 g*****g 的大作中提到】 : You can have your list as a field in another class, : say ImmutableList, and in this class you only expose : method that you want to expose. e.g. only the get(int i) : class ImmutableList { : private List data; : public Object get(int i) { : return data.get(i); : } : }
m*y
6 楼
Collections.unmodifiableList(List list)
【在 p***p 的大作中提到】 : I have writen some config data in a List and have to immutable reference, : how? or create a clone copy?