我在这里的任务很简单。这是整个作业的一个片段,但最终我需要在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
}
一个非常简单的例子,你要求的仍然是你的需求不清楚,所以我从中得到了什么,你需要在specifi中添加元素
其中提到,对于添加特定位置,有一种方法
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
如果你出现错误,那么首先检查你的列表是否有那么大的非大小
list.size();