从排序Arrays.asList()名单也改变了原点阵列? [英] Sort a list from Arrays.asList() changes also the origin array?

查看:213
本文介绍了从排序Arrays.asList()名单也改变了原点阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整理与 Arrays.asList检索列表时,发现一个奇怪的行为(对我来说)()。看来以后 Col​​lections.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 after Collections.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屋!

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