简体   繁体   English

如何将元素从一个有序整数ArrayList添加到另一个整数有序ArrayList?

[英]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.

相关问题 无法将元素添加到ArrayList <Integer> - Can not add elements to ArrayList<Integer> 如何添加一个ArrayList <Integer> 到ArrayList <ArrayList<Integer> &gt;一次又一次地使用ArrayList中的不同元素 <Integer> - How to add an ArrayList<Integer> to an ArrayList<ArrayList<Integer>> again and again with different elements in ArrayList<Integer> 我如何创建新的ArrayList <ArrayList<Integer> &gt;在不更改旧版本的情况下从旧版本中删除? - How can I create new ArrayList<ArrayList<Integer>> from an old one without changing the old one? 从有序的SQL将元素插入ArrayList - Insert Elements to an ArrayList from ordered SQL 在ArrayList中添加整数 <ArrayList<Integer> &gt; - Add Integer in ArrayList<ArrayList<Integer>> 如何将ArrayList转换为具有有序元素的String数组? - How to convert an ArrayList to String array with ordered elements? 我有一个包含ArrayList的ArrayList <Integer> 。 我无法从ArrayList中删除元素 <Integer> - I have an ArrayList that contains an ArrayList<Integer>. I am having trouble removing elements from the ArrayList<Integer> 如何在 ArrayList 中交换元素<arraylist<integer> &gt;? </arraylist<integer> - How to swap elements in ArrayList<ArrayList<Integer>>? 如何在二维ArrayList中的元素上添加(+)整数? - How can I add (+) an integer to an element in a 2-dimensional ArrayList? 如何将数组添加到数组的数组列表(Arraylist <Integer[]> ) - How do I add arrays to an Arraylist of Arrays (Arraylist<Integer[]>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM