[英]How can I add elements from one ordered integer ArrayList to another integer ordered ArrayList?
The task is to add elements from one ordered integer ArrayList to another and the elements in the result array should be in order too. 任务是将一个有序整数ArrayList中的元素添加到另一个中,结果数组中的元素也应有序。 It should work in the most effective way.
它应该以最有效的方式工作。
My Logic is this: 我的逻辑是这样的:
A.addAll(B);
Collections.sort(A);
But I'm not really sure this is the best solution. 但是我不太确定这是最好的解决方案。
From the documentation for ArrayList.addAll() 从ArrayList.addAll()的文档中
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
按照指定集合的Iterator返回的顺序,将指定集合中的所有元素追加到此列表的末尾。
My takeaway from this is that sorting the result array after adding your elements makes no sense, as the elements from your original array will appear in the same order in the result array as they originally were in. 我对此的理解是,添加元素后对结果数组进行排序是没有意义的,因为原始数组中的元素在结果数组中的显示顺序与最初的顺序相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.