从排序Arrays.asList()名单也改变了原点阵列? [英] Sort a list from Arrays.asList() changes also the origin array?
问题描述
我整理与 Arrays.asList检索列表时,发现一个奇怪的行为(对我来说)()
。看来以后 Collections.sort(名单)
,原点阵列还排序!
这怎么可能?
列表<数>率= Arrays.asList(arrayRates);
Collections.sort(费率,新RateEffectiveDateComparator());
/ *该费率清单与arrayRates阵列以相同的方式被排序之后* /
从Arrays.asList()的文档:
返回由指定数组支持的固定大小的列表。 (更改返回列表写到数组。)
块引用>您传递数组是数组列表是基于。当排序列表中,你实际上是排序的数组。检查
Arrays.asList源$ C $ C()
...I noticed a strange behavior (for me) when sorting a list retrieved with
Arrays.asList()
. It seems that afterCollections.sort( list )
, the origin array is also sorted !How is it possible ?
List<Rate> rates = Arrays.asList( arrayRates ); Collections.sort( rates, new RateEffectiveDateComparator() ); /* after that the rates list AND arrayRates array are sorted in the same way */
解决方案From the documentation of Arrays.asList():
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
The array you pass will be the array the list is based on. When sorting the list, you are actually sorting the array. Check the sourcecode of
Arrays.asList()
...这篇关于从排序Arrays.asList()名单也改变了原点阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!