2014-11-06 34 views
-1

我的任務很簡單。這是整個任務的一個片段,但最終我需要在指示位置指示的位置插入一個軌道到LinkedList中。但是,當我運行我的代碼檢查器時,出現以下錯誤。不明原因的「找不到符號」錯誤;符號似乎被正確定義

運轉試驗

編譯器錯誤:

/data/opt/codecheck/submissions/1411052126707049920533155043/Album.java:111: error: cannot find symbol 
     tracks.add(index, t); 
       ^
    symbol: variable index 
    location: class Album 
1 error 

任何想法?我不知道爲什麼這有問題。這些是我的指示和我的兩行代碼。謝謝。

你所要求的
/** 
* addTrackAt. Insert a track into the LinkedList 
* at the position indicated index. 
* 
* @param index where to insert 
* @param t the track to insert 
*/ 
public void addTrackAt(int index, Track t) //provided 
{ 
    tracks.add(index, t); // my code - but it's not working. I don't know why. 
} 
/** 
* removeTrackAt. Remove a track at a specific index. 
* 
* @param index the index at which to remove 
*/ 
public void removeTrackAt(int index) // provided 
{ 
    tracks.remove(index); // my code 
} 

/** 
* getTrackAt. Return the track at the given index. 
* 
* @param index the index at which to return 
* @return 
*/ 
public Track getTrackAt(int index) //provided by instructor 
{ 
    return tracks.get(index); // my code 
} 
+0

多尺寸的錯誤意味着沒有什麼叫在這方面「指標」,但這個錯誤不會對應於你發佈的代碼,你*做的*有一個名爲「index」的變量 - 它看起來*確定。您是否肯定第111行是您發佈的代碼段中的行?你確定你沒有犯過一個錯誤,比如在編輯它們之前不把你的文件保存在你的編輯器中?你確定你的「代碼檢查器」正在使用你的文件的最新版本嗎? – 2014-11-06 06:01:04

+1

第111行對應於tracks.add(index,t),代碼檢查器代碼對應於我的版本。基本上,我將我的教授指定的框架複製並粘貼到Eclipse上,然後在代碼檢查器中填入插槽。 – Eva 2014-11-06 06:11:12

+0

@Eva Eclipse中的一個常見問題是在運行之前不保存工作,因爲它不自動保存。確保保存所有文件,重新提交併檢查結果是否相同。我沒有任何其他建議,因爲它代表你發佈的代碼看起來很好,不會產生你描述的錯誤。有些地方是關閉的,這裏沒有足夠的信息來說明它是什麼。 – 2014-11-06 06:16:21

回答

0

一個很簡單的例子,還是你的要求是不是清楚,所以我從你需要添加元素specifi See this

,其中提到,添加特定位置有一個有way

void add(int index, E element) 
Inserts the specified element at the specified position in this list. 

public static void main(String args[]) { 
     List<String> list = new LinkedList<String>(); 
     list.add("Krishna"); 
     list.add("Krishna1"); 
     list.add("Krishna2"); 
     list.add("Krishna3"); 
     list.add("Krishna4"); 

     for (int i = 0; i < list.size(); i++) { 
      System.out.println(list.get(i)); 
     } 

     // now adding element 
     list.add(5, "Krishna5"); 

     for (int i = 0; i < list.size(); i++) { 
      System.out.println(list.get(i)); 
     } 
    } 

希望能對你有所幫助。

OR 

和如果u得到錯誤,那麼先檢查你的列表是具有不

list.size(); 
+0

這似乎與OP正試圖診斷的錯誤信息完全不相關,即「無法找到符號:索引」。 – 2014-11-06 06:06:06

+0

如何將元素添加到指定位置的LinkedList中,請參閱問題/ – Krishna 2014-11-06 06:06:42

+1

我確實看到了問題。這不僅僅是標題。 – 2014-11-06 06:07:04