Stack<?>[] stacks = {
new Stack<Bed>(),
new Stack<Bookshelves>(),
new Stack<Chair>(),
new Stack<Desk>(),
new Stack<Table>()
};
这就是制作堆栈数组的代码。我将它们放在一个数组中的原因是因为它是分配的要求之一。该程序在没有数组的情况下工作。
此外,堆栈采用泛型,因为我必须创建自己的堆栈(也是分配的要求)。
while(sc.hasNext()){
temp = sc.nextLine().split(" ");
if(temp[0].equals("Bed")){
//beds.push(new Bed(temp[1], temp[2]));
stacks[0].push(new Bed(temp[1], temp[2]));
}else if(temp[0].equals("Table")){
//tables.push(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4]));
stacks[4].push(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4]));
}else if(temp[0].equals("Desk")){
//desks.push(new Desk(temp[1],temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4])));
stacks[3].push(new Desk(temp[1],temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4])));
}else if(temp[0].equals("Chair")){
//chairs.push(new Chair(temp[1], temp[2]));
stacks[2].push(new Chair(temp[1], temp[2]));
}else if(temp[0].equals("Bookshelves")){
//bookshelves.push(new Bookshelves(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), temp[5]));
stacks[1].push(new Bookshelves(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), temp[5]));
}else{
color = temp[0];
}
}
此代码从文本文件中获取信息并将对象推入堆栈。
我收到一条错误消息:
push(capture#627 of ?) in Stack<capture#627 of ?> cannot be applied to (Bed)
我创建的所有类都会重复此错误。
注释掉的部分是之前我为每个对象创建一个堆栈时工作的代码。
将所有内容都推入堆栈后,我无法将所有内容都放入数组中,因为不必要的中间变量将取出点。