【发布时间】:2014-04-28 02:47:41
【问题描述】:
我有一个我想编写的方法,它有一个整数堆栈作为参数,我想将这个堆栈中的所有内容推送到我在该方法中创建的整数数组中。我该怎么做?
public void test(StackADT<Integer> stack)
{
SimpleListADT<Integer> values = new ArraySimpleList<Integer>();
// I want to push the content of the stack in the parameter to the list
// using a loop; the stacks will contain integers but they will be represented
// as strings, not sure if I have the for loop set up correctly
for (int i = 0; i < stack.size();i++)
{
String s = stack.get(i);
values.addToRear(s);
// not sure what to put in the body of the loop here, I know the above is
// incorrect because there's no get(int) method for stacks, I just need a
// push in the right direction
}
}
【问题讨论】:
-
查看如何遍历集合。 SO 不是要求为您完成编码的地方。
-
什么数组?另外,您的 StackADT 是如何实现的?
pop()? -
StackADT不是 Java 定义的包,因此我们对它一无所知,无法帮助您使用它。 -
您需要告诉我们这些类的 API,因为它们不是标准的 Java 集合类。
SimpleListADT是否实现java.util.List?StackADT呢?
标签: java arrays stack push element