所以我嘗試通過完成實現(xiàn)來實現(xiàn) SLList 類:get(i)、set(i, x)、add(i, x)和remove(i)操作,每個操作都需要 O(1 + i) 時間。我的程序遇到的困難是添加和獲取方法。我不斷收到錯誤incompatible types: SLList<T>.Node cannot be converted to int,而且incompatible types: SLList<T>.Node cannot be converted to int。我對如何修復它們感到非常困惑。我今天剛剛了解了鏈表,我正在努力理解它們的概念。任何幫助或提示將非常感激。public T get(int i) { // TODO: Implement this Node u = head; for(int j = 0; j < i; j++){ i = u.next; } return u; if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); return null;}public void add(int i, T x) { Node u = new Node(); u.x = x; if (i == 0) { head = u; } else { tail.next = u; } tail = u; i++; return true; if (i < 0 || i > n) throw new IndexOutOfBoundsException();}我應該提到每個函數(shù) T 和 void 的類型必須保持原樣。我還相信我應該在代碼中包含 IndexOutOfBoundsException 部分。如果你們想查看我的完整代碼,請訪問:https://pastebin.com/nJ9iMjxj
添加回答
舉報
0/150
提交
取消