对 Arrays.asList() 中的列表进行排序也会更改原始数组吗?
在对使用 Arrays.asList() 检索到的列表进行排序时,我注意到一个奇怪的行为(对我来说)。看来在 Collections.sort( list )
之后,原始数组也已排序!
怎么可能?
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 */
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 */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Arrays.asList() 的文档:
您传递的数组将是列表所基于的数组。对列表进行排序时,实际上是在对数组进行排序。检查
Arrays.asList()
的源代码...From the documentation of Arrays.asList():
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()
...