Stack<?>[] stacks = {
new Stack<Bed>(),
new Stack<Bookshelves>(),
new Stack<Chair>(),
new Stack<Desk>(),
new Stack<Table>()
};
這就是製作堆棧數組的代碼。我將它們放入數組的原因是因爲它是Assignment的要求之一。該程序沒有數組。JAVA通配符捕獲錯誤,使用通用堆棧數組
另外,堆棧採用泛型,因爲我必須創建自己的堆棧(也是Assignment的一個要求)。
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)
此錯誤重複了,我已經創建的所有類別。
註釋掉的部分是我爲每個對象製作單個堆棧之前的代碼。
將所有內容都推入堆棧之後,我無法將所有內容都放入數組中,因爲點將被取出用於不必要的中間變量。
試過了,沒有工作。另外,分配時不允許執行'b'選項。 – prunes4u 2012-03-26 17:35:16
我的意思是,當我說這些只是正確或類型安全地做到這一點的方法。另一種方法是丟失類型安全並開始進行不安全的原始轉換:'(Stack)stacks [0] .push'將採用任何'Object'。 Ewwwwwwwwww。 – 2012-03-27 00:15:18
是的,我不確定爲什麼一個任務會需要這個。似乎它教導的設計不佳。 – 2012-03-27 00:33:11