Java 不可修改数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java unmodifiable array
提问by Emil
final Integer[] arr={1,2,3};
arr[0]=3;
System.out.println(Arrays.toString(arr));
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.
我尝试了上面的代码来查看是否可以重新分配最终数组的变量[ ans:it can be]。我知道通过最终的 Integer[] 数组意味着我们不能分配除我们拥有的 Integer[] 的另一个实例之外的另一个实例最初分配。我想知道是否可以使数组变量也不可修改。
回答by Andre Holzner
This isn't possible as far as I know.
据我所知,这是不可能的。
There is however a method Collections.unmodifiableList(..)which creates an unmodifiable view of e.g. a List<Integer>
.
然而,有一个方法Collections.unmodifiableList(..)可以创建一个不可修改的视图,例如List<Integer>
.
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.
如果您想保证即使是不可修改视图列表的创建者也无法修改底层(可修改)列表,请查看Guava 的 ImmutableList。
回答by Brian Agnew
No. The contents of an array can be changed. You can't prevent that.
不可以。数组的内容是可以改变的。你无法阻止。
Collectionshas various methods for creating unmodifiable collections, but arrays aren't provided for.
集合有多种方法来创建不可修改的集合,但不提供数组。
回答by Ssancho
The final keyword only prevents changing the arr reference, i.e. you can't do:
final 关键字只能防止更改 arr 引用,即您不能这样做:
final int[] arr={1,2,3};
arr = new int[5];
If the object arr is referring to is mutable object (like arrays), nothing prevents you from modifying it.
如果 arr 所指的对象是可变对象(如数组),则没有什么可以阻止您修改它。
The only solution is to use immutable objects.
唯一的解决方案是使用不可变对象。
回答by xyz
Another way is to use this function:
另一种方法是使用这个函数:
Arrays.copyOf(arr, arr.length);
回答by Rajendra
The keyword 'final' applies to only the references (pointer to the memory location of the object in the heap). You can't change the memory address (location) of the object. Its upto your object how it internally handles the immutability.
关键字“final”仅适用于引用(指向对象在堆中的内存位置的指针)。您无法更改对象的内存地址(位置)。它取决于您的对象如何在内部处理不变性。
Added, although int is a primitive data type int[] should be treated as a object.
补充,虽然 int 是原始数据类型 int[] 应该被视为一个对象。
You can't do this
你不能这样做
final int a = 5
a = 6
You can do this:
你可以这样做:
final int[] a = new int[]{2,3,4};
a[0] = 6;
You can't do this:
你不能这样做:
final int[] a = new int[]{2,3,4};
a = new int[]{1,2,3}
回答by tmn
To anybody else reading this old question, keep in mind there is also Google Guava's immutable collections. ImmutableList has much stronger performance than Collections.unmodifiableList(), and is arguably safer as it truly is immutable,and not backed by a mutable collection.
对于阅读这个老问题的其他人,请记住还有 Google Guava 的不可变集合。ImmutableList 的性能比 Collections.unmodifiableList() 强得多,并且可以说更安全,因为它确实是不可变的,并且不受可变集合的支持。