反转堆栈并添加到 ArrayList 的最有效方法 [英] Most efficient way to reverse a stack and add to an ArrayList
问题描述
我有两个集合 - 一个 ArrayList 和一个 Stack.我使用堆栈是因为我需要一些简单的弹出/推送功能来处理这段代码.ArrayList 本质上是 out 变量,因为这是函数中的一小段代码.
I have two collections - an ArrayList and a Stack. I use the stack because I needed some simple pop/push functionality for this bit of code. The ArrayList is essentially the out variable as this is a small section of code in the function.
因此,变量被定义为这样,然后运行代码以将元素添加到堆栈中.
So, the variables are defined as such, then code is run to add elements to the stack.
ArrayList<String> out = new ArrayList<String>();
/* other code.. */
Stack<String> lineStack = new Stack<String>();
/* code that adds stuff to the stack */
问题是,既然我有一个完全填充的堆栈,我如何将它以与弹出顺序相反的顺序放置在 out ArrayList 中.
The question is, now that I have a fully populated stack, how do I place it in the out ArrayList in a reverse order then from the pop order.
我第一个想到的解决方案是
My first thought up solution was
while(!lineStack.empty()) {
out.add(0, lineStack.pop());
}
... 可行,但我担心将元素添加到 ArrayList 开头的效率(这会强制所有现有元素需要移动......它是一个链表(我相信)......大不了..但仍然是一个问题).此外,我正在通过一个循环运行它......也许是不必要的.
... which works, but I worry about the efficiency of adding an element to the beginning of the ArrayList (which forces all existing elements to need to shift.. it's a linked list (I believe).. big deal.. but still a concern). Also, I am running this through a loop... perhaps unnecessarily.
所以,我的第二个解决方案不涉及循环(至少在我的代码中,我确信后端调用正在这样做).
So, my second solution that didn't involve looping (at least in my code, i'm sure the back end calls are doing it).
List l = lineStack.subList(0, lineStack.size());
out.addAll(l);
我知道我不需要分配列表,但它会保留以获得更清晰的代码.但是,我不确定这是否会给我带来特别有用的性能提升.
I know I don't need to allocate the list, but it'll keep for cleaner code. However, I am not sure if this will give me a particularly helpful performance gain.
所以,我的问题是:对于小到中尺寸的套装,哪些可能最有效?如果有更有效的解决方案,那会是什么?
So, my question is: Which of these will likely be most efficient for SMALL to MEDIUM size sets? If there is a more efficient solution, what would it be?
推荐答案
Iterable
的Stack
实现顺序无论如何都要按照你想要的顺序进行,所以你可以使用
The Iterable<T>
implementation order of Stack<T>
goes in the order you want anyway, so you can just use
new ArrayList<String>(stack);
这是一个简短但完整的示例:
Here's a short but complete example:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Stack<String> stack = new Stack<String>();
stack.push("Bottom");
stack.push("Middle");
stack.push("Top");
List<String> list = new ArrayList<String>(stack);
for (String x : list)
{
System.out.println(x);
}
}
}
打印出来:
Bottom
Middle
Top
(这与弹出它们的顺序相反).
(which is the opposite order to what you'd get if you popped them).
另一个问题 - 你真的需要在 ArrayList
中使用它吗?Stack
实现了List
;您需要 ArrayList
的哪些特殊功能?(我不是说你不需要,只是检查一下!)
One other question - do you really need it in an ArrayList<String>
anyway? Stack<T>
implements List<T>
; what special features of ArrayList
do you need? (I'm not saying you don't need them, just checking!)
这篇关于反转堆栈并添加到 ArrayList 的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!