Java的不可修改的数组 [英] Java unmodifiable array

查看:556
本文介绍了Java的不可修改的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

final Integer[] arr={1,2,3};
arr[0]=3;
System.out.println(Arrays.toString(arr));

我试过上述code看到最终的数组变量是否可以重新分配[:它可以]。我明白,最后的整数[]数组这意味着我们不能分配整数[除了我们已经指派initially.I想知道是否有可能使数组变量也不可修改一]的另一个实例。

I tried the above code to see whether a final array's variables can be reassigned[ans:it can be].I understand that by a final Integer[] array it means we cannot assign another instance of Integer[] apart from the one we have assigned initially.I would like to know if whether it is possible to make the array variables also unmodifiable.

推荐答案

这是不可能的,据我所知。

This isn't possible as far as I know.

不过有一个方法<一href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#unmodifiableList%28java.util.List%29\"相对=nofollow> Col​​lections.unmodifiableList(..)创造的例如不可修改视图一个列表与LT;整数GT;

There is however a method Collections.unmodifiableList(..) which creates an unmodifiable view of e.g. a List<Integer>.

如果你想保证甚至没有修改视图列表的创建者将能够修改基础(修改)列表,看看<一个href=\"http://guava-libraries.google$c$c.com/svn-history/r12/trunk/javadoc/com/google/common/collect/ImmutableList.html\"相对=nofollow>番石榴的ImmutableList 。

If you want to guarantee that not even the creator of the unmodifiable view list will be able to modify the underlying (modifiable) list, have a look at Guava's ImmutableList.

这篇关于Java的不可修改的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆