-4
這是我在我的Josephus類中添加的方法,我應該使用一個循環鏈表。我在我的課的幾個領域得到空指針異常,但都是因爲這種方法。任何人都可以通過查看這段代碼看到任何顯而易見的錯誤嗎?這個Java代碼有什麼問題
/** Inserts the specified element in the list at the
last position
@param dataItem the element to add
*/
// Complexity O(1)
@SuppressWarnings({ "unchecked" })
public void add(E dataItem) {
Node <E> node = new Node <E> (dataItem,null,null);
if (count == 0){ // list is empty
head = node.previous= node;
}
else {
head.previous.next = node;
node.previous = head.previous;
head.previous = node;
}
count++;
}
全碼:http://pastebin.com/k4Hmbqmw
有得多未知變量。所以它很容易看到問題。 – rekire
你在其他部分使用花括號嗎? –
重要的是你在if和else塊周圍缺少'{}'。 –