简体   繁体   中英

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. 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()

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM