最有效的方法来反转堆栈并添加到ArrayList [英] Most efficient way to reverse a stack and add to an ArrayList
问题描述
所以,变量被定义为这样,然后运行代码来添加元素
ArrayList< String> out = new ArrayList< String>();
/ *其他代码.. * /
堆栈< String> lineStack = new Stack< String>();
/ *向堆栈添加东西的代码* /
是的,现在我有一个完全填充的堆栈,我如何将它放在ArrayList中,然后从弹出式命令。
我的第一个想到的解决方案是
while(!lineStack.empty()){
out.add(0,lineStack.pop() );
}
...它有效,但我担心添加元素的效率到ArrayList的开始(这迫使所有现有的元素需要移动..它是一个链表(我相信)..大事..但仍然是一个关注)。此外,我正在循环运行...或许不必要。
所以,我的第二个解决方案并不涉及循环(至少在我的代码中,我确定后端调用正在执行)。
列表l = lineStack.subList(0,lineStack.size());
out.addAll(l);
我知道我不需要分配列表,但它会保持更清晰的代码。但是,我不知道这是否会给我一个特别有用的性能提升。
所以,我的问题是:其中哪些可能是最有效的SMALL到MEDIUM尺寸集?
Iterable< T> / code>执行顺序
堆栈< T>
按照你想要的顺序进行,所以你可以使用
new ArrayList< String>(stack);
以下是一个简短但完整的示例:
import java.util。*;
public class Test
{
public static void main(String [] args)
{
堆栈< String> stack = new Stack< String>();
stack.push(Bottom);
stack.push(Middle);
stack.push(Top);
列表< String> list = new ArrayList< String>(stack);
(String x:list)
{
System.out.println(x);
}
}
}
打印出来: p>
底部
中间
顶部
(这是与您弹出时相同的顺序)。
编辑:另一个问题 - 你真的需要它在 ArrayList< String>
? 堆栈< T>
实现列表< T>
您需要什么特殊功能 ArrayList
? (我不是说你不要需要他们,只需检查!)
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 */
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());
}
... 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?
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);
}
}
}
This prints out:
Bottom
Middle
Top
(which is the opposite order to what you'd get if you popped them).
EDIT: 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屋!