无法在Java中显示foreach循环(Stack)中的所有项



我想显示堆栈中的所有项,但它不起作用。

这是我的课程:

public class CommandFactory
{
public Command getCommand(String type)
{
if(type.equalsIgnoreCase("c"))
{
return new CreateDVD();
}
//......Other commands
}  
return null;
}
public interface DVD
{
int getDvdID();
String getTitle();
int getLength();
int getNumAvailable();
void setNumAvailable(int numAvailable);
String toString();
} 
public class Movie implements DVD
{
private int dvdID;
private String title;
private int length;
private int numAvailable;
private String director;
public Movie(int dvdID, String title, int length, int numAvailable, String director)
{
//Constructor
}
public int getDvdID() { return dvdID; }
//...getters and setters
public String toString() 
{
return String.format("DVD InformationnID: %dnTitle: %snLength: %d minsnNumber of available copies: %dnDirector: %s", dvdID, title, length, numAvailable, director);
}
}
public class CreateDVD implements Command
{
public Stack<DVD> dvdStack = new Stack<>();
private Stack<DVD> dvdUndoStack = new Stack<>();
private DVD dvd;
private DVDFactory dvdFactory = new DVDFactory();
private Scanner kb = new Scanner(System.in);
public void execute()
{
System.out.println("Please enter DVD type (mo=movie/mv=MV)");
String type = kb.next();
dvd = dvdFactory.getDVD(type);
dvdStack.push(dvd);
}
//other methods 
}

下面的实现将只显示堆栈中的最后一项。

public class DVDHandler
{
Stack<Command> unDoCommandStack = new Stack<>();
Stack<Command> reDoCommandStack = new Stack<>();
Stack<DVD> dvdStack = new Stack<>();
CommandFactory commandFactory = new CommandFactory();
boolean exit = false;
public void run()
{
.
.
.
Scanner kb = new Scanner(System.in);
String input = kb.next();
Command command = commandFactory.getCommand(input);
if(command instanceof CreateDVD)
{
command.execute();
dvdStack = ((CreateDVD) command).getDvdStack();
unDoCommandStack.push(command);
}
.
.
.
else if(input.equalsIgnoreCase("s"))
{
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdStack)
{
System.out.println(dvd.toString());
}
}
}
}
}

我试着打印出dvdStack,那里有多个项目。我不知道为什么它不起作用。

然后,我使用下面的ArrayList实现了它:

ArrayList<DVD> dvdArrayList = new ArrayList<>();
.
.
.
if(command instanceof CreateDVD)
{
command.execute();
dvdArrayList.add( (CreateDVD) command.getDvd() )
unDoCommandStack.push(command);
}
.
.
.
System.out.println("Enter ID(enter a to show all): ");
String input1 = kb.next();
if(input1.equalsIgnoreCase("a"))
{
for (DVD dvd:dvdArrayList)
{
System.out.println(dvd.toString());
}
}

此实现适用于显示所有项目。然而,我被告知要使用stack。

有人能帮我显示堆叠中的所有项目吗?

每次输入"c"时,都会创建一个新的CreateDVD命令,该命令有一个空堆栈。

然后执行它,将一张DVD添加到它的堆栈中。

然后DVDHandler的堆栈被CreateDVD命令的堆栈替换:

dvdStack = ((CreateDVD) command).getDvdStack();

因此,在堆栈中只找到一张DVD并不奇怪。

下次,请发布更多代码,以便我们能够更清楚地帮助解决这个问题。然而,根据您在这里的了解,一个可能的解决方案是使用for循环和stack.elementAt方法。例如:

public static void stackAttack (int inter){
Stack<Integer> stack=new Stack<Integer>();
stack.push(99);
stack.push(88);
stack.push(77);
stack.push(inter);
for(int n=stack.size(); n>0; n--) {
System.out.println(stack.elementAt(n-1));
}
}
public static void main (String[] args) {
stackAttack(69);
}

将以后进先出法的顺序打印出堆栈中的每个元素,而不会缩小它。希望这有帮助,下次请提供更多信息。

最新更新